☕️JAVA Language Community
2.91K subscribers
144 photos
7 videos
31 files
42 links
☕️ Software, IT, Java, news
💻 IT highlights
🎯 AI update
🖥⌨️🖱
Download Telegram
#Design_Patterns

#Factory_Method_Design_Pattern

👉Intent

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
Defining a "virtual" constructor.
The new operator considered harmful.

@javaCode☕️
#Java_Interview_Question


41) Can we override static method?

No, you can't override the static method because they are the part of class not object.

@javaCode☕️
#Java_Interview_Question


42) Why we cannot override static method?

It is because the static method is the part of class and it is bound with class whereas instance method is bound with object and static gets memory in class area and instance gets memory in heap.

@javaCode☕️
#Design_Patterns

#Factory_Method_Design_Pattern

👉Problem

A framework needs to standardize the architectural model for a range of applications, but allow for individual applications to define their own domain objects and provide for their instantiation.
—•—•—•—•—•—•—•—•—•—

👉Discussion

Factory Method is to creating objects as Template Method is to implementing an algorithm. A superclass specifies all standard and generic behavior (using pure virtual "placeholders" for creation steps), and then delegates the creation details to subclasses that are supplied by the client.

Factory Method makes a design more customizable and only a little more complicated. Other design patterns require new classes, whereas Factory Method only requires a new operation.

People often use Factory Method as the standard way to create objects; but it isn't necessary if: the class that's instantiated never changes, or instantiation takes place in an operation that subclasses can easily override (such as an initialization operation).

Factory Method is similar to Abstract Factory but without the emphasis on families.

Factory Methods are routinely specified by an architectural framework, and then implemented by the user of the framework.

@javaCode☕️
#Java_Interview_Question

43) Can we override the overloaded method?

Yes.

@javaCode☕️
#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☕️
#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☕️
#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☕️
The client is totally decoupled from the implementation details of derived classes. Polymorphic creation is now possible.

@javaCode☕️
#Java_Interview_Question

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☕️
☕️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☕️
#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☕️
#Java_Interview_Question

48) What is final method?

Final methods can't be overriden.

@javaCode☕️
#Java_Interview_Question

49) What is final class?

Final class can't be inherited.

@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☕️