#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☕️
#Design_Patterns
#Object_Pool_Design_Pattern
👉Problem
Object pools (otherwise known as resource pools) are used to manage the object caching. A client with access to a Object pool can avoid creating a new Objects by simply asking the pool for one that has already been instantiated instead. Generally the pool will be a growing pool, i.e. the pool itself will create new objects if the pool is empty, or we can have a pool, which restricts the number of objects created.
It is desirable to keep all Reusable objects that are not currently in use in the same object pool so that they can be managed by one coherent policy. To achieve this, the Reusable Pool class is designed to be a singleton class.
@javaCode☕️
#Object_Pool_Design_Pattern
👉Problem
Object pools (otherwise known as resource pools) are used to manage the object caching. A client with access to a Object pool can avoid creating a new Objects by simply asking the pool for one that has already been instantiated instead. Generally the pool will be a growing pool, i.e. the pool itself will create new objects if the pool is empty, or we can have a pool, which restricts the number of objects created.
It is desirable to keep all Reusable objects that are not currently in use in the same object pool so that they can be managed by one coherent policy. To achieve this, the Reusable Pool class is designed to be a singleton class.
@javaCode☕️
#Java_Interview_Question
59) Can there be any abstract method without abstract class?
No, if there is any abstract method in a class, that class must be abstract.
@javaCode☕️
59) Can there be any abstract method without abstract class?
No, if there is any abstract method in a class, that class must be abstract.
@javaCode☕️
#Java_Interview_Question
60) Can you use abstract and final both with a method?
No, because abstract method needs to be overridden whereas you can't override final method.
@javaCode☕️
60) Can you use abstract and final both with a method?
No, because abstract method needs to be overridden whereas you can't override final method.
@javaCode☕️