#Java_Interview_Question
44) Difference between method Overloading and Overriding.
1) Method overloading increases the readability of the program.
Method overriding provides the specific implementation of the method that is already provided by its super class.
2) method overlaoding is occurs within the class.
Method overriding occurs in two classes that have IS-A relationship.
3) In this case, parameter must be different.
In this case, parameter must be same.
@javaCode☕️
44) Difference between method Overloading and Overriding.
1) Method overloading increases the readability of the program.
Method overriding provides the specific implementation of the method that is already provided by its super class.
2) method overlaoding is occurs within the class.
Method overriding occurs in two classes that have IS-A relationship.
3) In this case, parameter must be different.
In this case, parameter must be same.
@javaCode☕️
#Design_Patterns
#Factory_Method_Design_Pattern
👉Structure
The implementation of Factory Method discussed in the Gang of Four (below) largely overlaps with that of Abstract Factory. For that reason, the presentation in this chapter focuses on the approach that has become popular since.
@javaCode☕️
#Factory_Method_Design_Pattern
👉Structure
The implementation of Factory Method discussed in the Gang of Four (below) largely overlaps with that of Abstract Factory. For that reason, the presentation in this chapter focuses on the approach that has become popular since.
@javaCode☕️
#Design_Patterns
#Factory_Method_Design_Pattern
An increasingly popular definition of factory method is: a static method of a class that returns an object of that class' type. But unlike a constructor, the actual object it returns might be an instance of a subclass. Unlike a constructor, an existing object might be reused, instead of a new object created. Unlike a constructor, factory methods can have different and more descriptive names (e.g. Color.make_RGB_color(float red, float green, float blue) and Color.make_HSB_color(float hue, float saturation, float brightness)
@javaCode☕️
#Factory_Method_Design_Pattern
An increasingly popular definition of factory method is: a static method of a class that returns an object of that class' type. But unlike a constructor, the actual object it returns might be an instance of a subclass. Unlike a constructor, an existing object might be reused, instead of a new object created. Unlike a constructor, factory methods can have different and more descriptive names (e.g. Color.make_RGB_color(float red, float green, float blue) and Color.make_HSB_color(float hue, float saturation, float brightness)
@javaCode☕️
The client is totally decoupled from the implementation details of derived classes. Polymorphic creation is now possible.
@javaCode☕️
@javaCode☕️
#Java_Interview_Question
45) Can you have virtual functions in Java?
Yes, all functions in Java are virtual by default.
@javaCode☕️
45) Can you have virtual functions in Java?
Yes, all functions in Java are virtual by default.
@javaCode☕️
#Java_Interview_Question
46) What is covariant return type?
Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type.
@javaCode☕️
46) What is covariant return type?
Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type.
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 46) What is covariant return type? Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type.…
More details:
👉Covariant Return Type
The covariant return type specifies that the return type may vary in the same direction as the subclass.
Before Java5, it was not possible to override any method by changing the return type. But now, since Java5, it is possible to override method by changing the return type if subclass overrides any method whose return type is Non-Primitive but it changes its return type to subclass type. Let's take a simple example:
class A{
A get(){
return this;
}
}
class B1 extends A{
B1 get(){
return this;
}
void message(
{ System.out.println("welcome to covariant return type");}
public static void main(String args[]){
new B1().get().message();
}
}
Output:welcome to covariant return type
As you can see in the above example, the return type of the get() method of A class is A but the return type of the get() method of B class is B. Both methods have different return type but it is method overriding. This is known as covariant return type.
@javaCode☕️
👉Covariant Return Type
The covariant return type specifies that the return type may vary in the same direction as the subclass.
Before Java5, it was not possible to override any method by changing the return type. But now, since Java5, it is possible to override method by changing the return type if subclass overrides any method whose return type is Non-Primitive but it changes its return type to subclass type. Let's take a simple example:
class A{
A get(){
return this;
}
}
class B1 extends A{
B1 get(){
return this;
}
void message(
{ System.out.println("welcome to covariant return type");}
public static void main(String args[]){
new B1().get().message();
}
}
Output:welcome to covariant return type
As you can see in the above example, the return type of the get() method of A class is A but the return type of the get() method of B class is B. Both methods have different return type but it is method overriding. This is known as covariant return type.
@javaCode☕️
#Java_Interview_Question
47) What is final variable?
If you make any variable as final, you cannot change the value of final variable(It will be constant).
@javaCode☕️
47) What is final variable?
If you make any variable as final, you cannot change the value of final variable(It will be constant).
@javaCode☕️
☕️JAVA Language Community
@javaCode☕️
#Design_Patterns
#Factory_Method_Design_Pattern
👉Example
The Factory Method defines an interface for creating objects, but lets subclasses decide which classes to instantiate. Injection molding presses demonstrate this pattern. Manufacturers of plastic toys process plastic molding powder, and inject the plastic into molds of the desired shapes. The class of toy (car, action figure, etc.) is determined by the mold.
@javaCode☕️
#Factory_Method_Design_Pattern
👉Example
The Factory Method defines an interface for creating objects, but lets subclasses decide which classes to instantiate. Injection molding presses demonstrate this pattern. Manufacturers of plastic toys process plastic molding powder, and inject the plastic into molds of the desired shapes. The class of toy (car, action figure, etc.) is determined by the mold.
@javaCode☕️
#Java_Interview_Question
50) What is blank final variable?
A final variable, not initalized at the time of declaration, is known as blank final variable.
@javaCode☕️
50) What is blank final variable?
A final variable, not initalized at the time of declaration, is known as blank final variable.
@javaCode☕️
#Java_Interview_Question
51) Can we intialize blank final variable?
Yes, only in constructor if it is non-static. If it is static blank final variable, it can be initialized only in the static block.
@javaCode☕️
51) Can we intialize blank final variable?
Yes, only in constructor if it is non-static. If it is static blank final variable, it can be initialized only in the static block.
@javaCode☕️
#Design_Patterns
#Factory_Method_Design_Pattern
👉Check list
1️⃣If you have an inheritance hierarchy that exercises polymorphism, consider adding a polymorphic creation capability by defining a static factory method in the base class.
2️⃣Design the arguments to the factory method. What qualities or characteristics are necessary and sufficient to identify the correct derived class to instantiate?
3️⃣Consider designing an internal "object pool" that will allow objects to be reused instead of created from scratch.
4️⃣Consider making all constructors private or protected.
@javaCode☕️
#Factory_Method_Design_Pattern
👉Check list
1️⃣If you have an inheritance hierarchy that exercises polymorphism, consider adding a polymorphic creation capability by defining a static factory method in the base class.
2️⃣Design the arguments to the factory method. What qualities or characteristics are necessary and sufficient to identify the correct derived class to instantiate?
3️⃣Consider designing an internal "object pool" that will allow objects to be reused instead of created from scratch.
4️⃣Consider making all constructors private or protected.
@javaCode☕️
#Java_Interview_Question
52) Can you declare the main method as final?
Yes, such as,
public static final void main(String[] args){}.
@javaCode☕️
52) Can you declare the main method as final?
Yes, such as,
public static final void main(String[] args){}.
@javaCode☕️