#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☕️
#Java_Interview_Question
53) What is Runtime Polymorphism?
Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time.
In this process, an overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.
@javaCode☕️
53) What is Runtime Polymorphism?
Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time.
In this process, an overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.
@javaCode☕️
#Design_Patterns
#Factory_Method_Design_Pattern
👉Rules of thumb
▪️Abstract Factory classes are often implemented with Factory Methods, but they can be implemented using Prototype.
▪️Factory Methods are usually called within Template Methods.
▪️Factory Method: creation through inheritance. Prototype: creation through delegation.
▪️Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
▪️Prototype doesn't require subclassing, but it does require an Initialize operation. Factory Method requires subclassing, but doesn't require Initialize.
▪️The advantage of a Factory Method is that it can return the same instance multiple times, or can return a subclass rather than an object of that exact type.
▪️Some Factory Method advocates recommend that as a matter of language design (or failing that, as a matter of style) absolutely all constructors should be private or protected. It's no one else's business whether a class manufactures a new object or recycles an old one.
▪️The new operator considered harmful. There is a difference between requesting an object and creating one. The new operator always creates an object, and fails to encapsulate object creation. A Factory Method enforces that encapsulation, and allows an object to be requested without inextricable coupling to the act of creation.
@javaCode☕️
#Factory_Method_Design_Pattern
👉Rules of thumb
▪️Abstract Factory classes are often implemented with Factory Methods, but they can be implemented using Prototype.
▪️Factory Methods are usually called within Template Methods.
▪️Factory Method: creation through inheritance. Prototype: creation through delegation.
▪️Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
▪️Prototype doesn't require subclassing, but it does require an Initialize operation. Factory Method requires subclassing, but doesn't require Initialize.
▪️The advantage of a Factory Method is that it can return the same instance multiple times, or can return a subclass rather than an object of that exact type.
▪️Some Factory Method advocates recommend that as a matter of language design (or failing that, as a matter of style) absolutely all constructors should be private or protected. It's no one else's business whether a class manufactures a new object or recycles an old one.
▪️The new operator considered harmful. There is a difference between requesting an object and creating one. The new operator always creates an object, and fails to encapsulate object creation. A Factory Method enforces that encapsulation, and allows an object to be requested without inextricable coupling to the act of creation.
@javaCode☕️
#Java_Interview_Question
55) What is the difference between static binding and dynamic binding?
In case of static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at runtime.
@javaCode☕️
55) What is the difference between static binding and dynamic binding?
In case of static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at runtime.
@javaCode☕️
#Java_Interview_Question
56) What is abstraction?
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Abstraction lets you focus on what the object does instead of how it does it.
@javaCode☕️
56) What is abstraction?
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Abstraction lets you focus on what the object does instead of how it does it.
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 56) What is abstraction? Abstraction is a process of hiding the implementation details and showing only functionality to the user. Abstraction lets you focus on what the object does instead of how it does it. @javaCode☕️
More details:
👉Abstraction in Java
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Another way, it shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
Ways to achieve Abstaction
There are two ways to achieve abstraction in java
1️⃣Abstract class (0 to 100%)
2️⃣Interface (100%)
@javaCode☕️
👉Abstraction in Java
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Another way, it shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.
Ways to achieve Abstaction
There are two ways to achieve abstraction in java
1️⃣Abstract class (0 to 100%)
2️⃣Interface (100%)
@javaCode☕️
#Design_Patterns
#Creational_patterns
3️⃣#Object_Pool_Design_Pattern
👉Intent
Object pooling can offer a significant performance boost; it is most effective in situations where the cost of initializing a class instance is high, the rate of instantiation of a class is high, and the number of instantiations in use at any one time is low.
@javaCode☕️
#Creational_patterns
3️⃣#Object_Pool_Design_Pattern
👉Intent
Object pooling can offer a significant performance boost; it is most effective in situations where the cost of initializing a class instance is high, the rate of instantiation of a class is high, and the number of instantiations in use at any one time is low.
@javaCode☕️
#Java_Interview_Question
57) What is the difference between abstraction and encapsulation?
Abstraction hides the implementation details whereas encapsulation wraps code and data into a single unit.
@javaCode☕️
57) What is the difference between abstraction and encapsulation?
Abstraction hides the implementation details whereas encapsulation wraps code and data into a single unit.
@javaCode☕️
#Java_Interview_Question
58) What is abstract class?
A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.
@javaCode☕️
58) What is abstract class?
A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.
@javaCode☕️