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

More Details:

👉Method Overloading in Java

▪️If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.

▪️If we have to perform only one operation, having same name of the methods increases the readability of the program.

▪️Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs.

👉Advantage of method overloading

Method overloading increases the readability of the program.

👉Different ways to overload the method

There are two ways to overload the method in java

1️⃣By changing number of arguments

2️⃣By changing the data type

@javaCode☕️
#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 creating all possible output representations. Capture the steps of this protocol in a Builder interface.

4️⃣Define a Builder derived class for each target representation.

5️⃣The client creates a Reader object and a Builder object, and registers the latter with the former.

6️⃣The client asks the Reader to "construct".

7️⃣The client asks the Builder to return the result.

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