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

38) Why method overloading is not possible by changing the return type in java?

Becauseof ambiguity.

@javaCode☕️
#Java_Interview_Question

39) Can we overload main() method?

Yes, You can have many main() methods in a class by overloading the main method.

@javaCode☕️
☕️JAVA Language Community
#Design_Patterns #Builder_Design_Pattern 👉Check list 1️⃣Decide if a common input and many possible representations (or outputs) is the problem at hand. 2️⃣Encapsulate the parsing of the common input in a Reader class. 3️⃣Design a standard protocol for…
#Design_Patterns

#Builder_Design_Pattern

👉Rules of thumb

▪️Sometimes creational patterns are complementary: Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.

▪️Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.

▪️Builder often builds a Composite.

▪️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.

@javaCode☕️
#Java_Interview_Question

40) What is method overriding:

If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the method.

@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 40) What is method overriding: If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific…
More details:

👉Usage of Java Method Overriding

▪️Method overriding is used to provide specific implementation of a method that is already provided by its super class.

▪️Method overriding is used for runtime polymorphism


👉Rules for Java Method Overriding

▪️method must have same name as in the parent class

▪️method must have same parameter as in the parent class.

▪️must be IS-A relationship (inheritance).

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