1) Can Constructor have a return type?
Ans: We should not , if a constructor have a return type, then that constructor
will become a normal Method.
2)Can we have a Synchronized keyword on a Constructor?
Ans: No, We should allow two threads to create a new Objects at same time.
3) What are the Restrictions on overriding a Method in Java?
Ans :
1) The Overriding method in the sub class should have the same return type.
Example :
Method in Super class:
protected String overrideme(){
System.out.println(" I am Over riding in my Sub Class");
return null;
}
Override Method in Sub-Class.
Observation : In Sub-class we trying to decrease the visibility from Protected to private.
Restriction 3 :
The overriding method must not throw new or broader checked exceptions.
Ans: We should not , if a constructor have a return type, then that constructor
will become a normal Method.
2)Can we have a Synchronized keyword on a Constructor?
Ans: No, We should allow two threads to create a new Objects at same time.
3) What are the Restrictions on overriding a Method in Java?
Ans :
1) The Overriding method in the sub class should have the same return type.
Example :
Method in Super class:
protected String overrideme(){
System.out.println(" I am Over riding in my Sub Class");
return null;
}
Override Method in Sub-Class.
protected void overrideme(){
System.out.println(" I am Over riding in my Sub Class");
}
Observation : The Return types are not matched.
Restriction 2 : Cannot reduce the visibility of the override method.
Method in super class:
protected String overrideme(){
System.out.println(" I am Over riding in my Sub Class");
return null;
}
Method in Sub Class :
private String overrideme(){
System.out.println(" Method Overriding");
return null;
}
Observation : In Sub-class we trying to decrease the visibility from Protected to private.
Restriction 3 :
The overriding method must not throw new or broader checked exceptions.
Example : Method in Super class :
public static FileInputStream f1(String fileName)
throws FileNotFoundException
{
FileInputStream fis = new FileInputStream(fileName);
System.out.println("f1: File input stream created");
return fis;
}
Example in Method in sub class :
public static FileInputStream f1(String fileName)
throws ClassNotFoundException
{
FileInputStream fis = new FileInputStream(fileName);
System.out.println("f1: File input stream created");
return fis;
}
Observation : Here the overriden method in the sub-class throwing the
different checked exception from the Super class.
It should not throw a different or new checked Exception in sub class.
Note: But these is not the case with un-checked Exceptions.
Question : What is Virtual and Non Virtual in related to C++ in java?
Ans: In java all methods are default Virtual we can make it non-virtual by adding the
keyword final before it.
consider this Example :
package newPackage;
class Base {
// virtual by default
public void show() {
System.out.println("Base::show() called");
}
}
class Derived extends Base {
public void show() {
System.out.println("Derived::show() called");
}
}
public class ThreadPool {
public static void main(String[] args) {
Base b = new Derived();
b.show();
}
}
We can make it non virtual by adding final keyword.
final (non-virtual Methods) cannot be overridden.
Question: Explain the Abstraction OOP concept with Example?
Ans:
Abstraction is the process of reducing the object to it's essence, so that only the
necessity elements can be represented.
Abstract defines an object in-terms of it's attribute and behavior.
Example of Customer:
Requirements of customer from customer side.
1) customer name, customer id , password.address.account number and branch.
from these requirements we can create a three class.
class1:
customer class: contains customer-id, customer name,password.
class2:
address class : contains country,pin,street.
class3:
account details : contains branch,bank, account number,account status.
Advantages of Abstraction :
To reduce reality complex, and design should be the loosely coupled.
in the above we are segregating the all necessary information into 3 classes, is called
the loosely coupled.
Abstraction talks more about the design,how we are going to implement.
Example of Abstraction : TV remote, the customer can see only the buttons, but does not
care about the chip and all.
Abstraction means hiding the complexity of data or unnecessary data.
Abstraction solves the problem in the design.
Abstraction is about ignoring the things that don't matter so you can focus on the things
that do matter.
Explain the Difference between C++ and Java Programming language?
Ans :
Differences :
1) java Does not support the keywords : typedef and or preprocessor.
2) Global functions and global data not allowed in Java.
3)In java all classes are inherited from java, there is no significant concept in C++.
4)The interface concept does not support in C++.
5)Java does not support multiple inheritance.
6) Java Does not support goto statement, in the place it support's labeled break and
continue statements.
7) Java does not support Operator overloading.
8) Unlike c++ java string's are immutable.
9)Conditional expressions in Java must evaluate to boolean rather than to integer, as is
the case in C++. Statements such as if(x+y)... are not allowed in Java because the conditional expression doesn't evaluate to a boolean.
Similarities :
1) Both java and C++ support the static methods and static variables.
2)Like C++, Java supports constructors that may be overloaded.
3)Like C++, Java allows you to overload functions. However, default arguments are not
supported by Java.
Question : What are the Restrictions in overloading a Method?
Answer : 1_) Must change the argument list.
2_) can change the return type.
3_) can change the access modifier.
4_) can declare a new or a border checked Exception.
Note : We cannot Override the static methods.
We cannot override the final Methods.
Note: Method Overloading cannot be done based only on the return type.
Note: And Method Overloading cannot be done based only on the Access specifier.
Example :
public void call(){
}
private void call(){
}
Show's a checked Exception saying that Duplicate Methods.
No comments:
Post a Comment