#Design_Patterns
#Builder_Design_Pattern
👉Example
The Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. This pattern is used by fast food restaurants to construct children's meals. Children's meals typically consist of a main item, a side item, a drink, and a toy (e.g., a hamburger, fries, Coke, and toy dinosaur). Note that there can be variation in the content of the children's meal, but the construction process is the same. Whether a customer orders a hamburger, cheeseburger, or chicken, the process is the same. The employee at the counter directs the crew to assemble a main item, side item, and toy. These items are then placed in a bag. The drink is placed in a cup and remains outside of the bag. This same process is used at competing restaurants.
@javaCode☕️
#Builder_Design_Pattern
👉Example
The Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. This pattern is used by fast food restaurants to construct children's meals. Children's meals typically consist of a main item, a side item, a drink, and a toy (e.g., a hamburger, fries, Coke, and toy dinosaur). Note that there can be variation in the content of the children's meal, but the construction process is the same. Whether a customer orders a hamburger, cheeseburger, or chicken, the process is the same. The employee at the counter directs the crew to assemble a main item, side item, and toy. These items are then placed in a bag. The drink is placed in a cup and remains outside of the bag. This same process is used at competing restaurants.
@javaCode☕️
#Java_Interview_Question
35) Can you use this() and super() both in a constructor?
No. Because super() or this() must be the first statement.
@javaCode☕️
35) Can you use this() and super() both in a constructor?
No. Because super() or this() must be the first statement.
@javaCode☕️
#Java_Interview_Question
36)What is object cloning?
The object cloning is used to create the exact copy of an object.
@javaCode☕️
36)What is object cloning?
The object cloning is used to create the exact copy of an object.
@javaCode☕️
#Java_Interview_Question
More Details:
👉Object Cloning in Java
▪️The object cloning is a way to create exact copy of an object. For this purpose, clone() method of Object class is used to clone an object.
▪️The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException.
The clone() method is defined in the Object class. Syntax of the clone() method is as follows:
✏️protected Object clone() throws CloneNotSupportedException
❓Why use clone() method ?
The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing to be performed that is why we use object cloning.
❗️Advantage of Object cloning
Less processing task.
@javaCode☕️
More Details:
👉Object Cloning in Java
▪️The object cloning is a way to create exact copy of an object. For this purpose, clone() method of Object class is used to clone an object.
▪️The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException.
The clone() method is defined in the Object class. Syntax of the clone() method is as follows:
✏️protected Object clone() throws CloneNotSupportedException
❓Why use clone() method ?
The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing to be performed that is why we use object cloning.
❗️Advantage of Object cloning
Less processing task.
@javaCode☕️
☕️JAVA Language Community
Photo
🔴C++ vs Java
There are many differences and similarities between C++ programming language and Java. A list of top differences between C++ and Java are given below:
▪️Platform-independent
C++ is platform-dependent.
Java is platform-independent.
▪️Mainly used for
C++ is mainly used for system programming.
Java is mainly used for application programming. It is widely used in window, web-based, enterprise and mobile applications.
▪️Goto
C++ supports goto statement.
Java doesn't support goto statement.
▪️Multiple inheritance
C++ supports multiple inheritance.
Java doesn't support multiple inheritance through class. It can be achieved by interfaces in java.
▪️Operator Overloading
C++ supports operator overloading.
Java doesn't support operator overloading.
▪️Pointers
C++ supports pointers. You can write pointer program in C++. Java supports pointer internally. But you can't write the pointer program in java. It means java has restricted pointer support in java.
▪️Compiler and Interpreter
C++ uses compiler only.
Java uses compiler and interpreter both.
▪️Call by Value and Call by reference
C++ supports both call by value and call by reference.
Java supports call by value only. There is no call by reference in java.
▪️Structure and Union
C++ supports structures and unions.
Java doesn't support structures and unions.
▪️Thread Support
C++ doesn't have built-in support for threads. It relies on third-party libraries for thread support. Java has built-in thread support.
▪️Documentation comment
C++ doesn't support documentation comment.
Java supports documentation comment (/** ... */) to create documentation for java source code.
▪️Virtual Keyword
C++ supports virtual keyword so that we can decide whether or not override a function.
Java has no virtual keyword. We can override all non-static methods by default. In other words, non-static methods are virtual by default.
▪️unsigned right shift >>>
C++ doesn't support >>> operator.
Java supports unsigned right shift >>> operator that fills zero at the top for the negative numbers. For positive numbers, it works same like >> operator.
▪️Inheritance Tree
C++ creates a new inheritance tree always.
Java uses single inheritance tree always because all classes are the child of Object class in java. Object class is the root of inheritance tree in java.
@javaCode☕️
There are many differences and similarities between C++ programming language and Java. A list of top differences between C++ and Java are given below:
▪️Platform-independent
C++ is platform-dependent.
Java is platform-independent.
▪️Mainly used for
C++ is mainly used for system programming.
Java is mainly used for application programming. It is widely used in window, web-based, enterprise and mobile applications.
▪️Goto
C++ supports goto statement.
Java doesn't support goto statement.
▪️Multiple inheritance
C++ supports multiple inheritance.
Java doesn't support multiple inheritance through class. It can be achieved by interfaces in java.
▪️Operator Overloading
C++ supports operator overloading.
Java doesn't support operator overloading.
▪️Pointers
C++ supports pointers. You can write pointer program in C++. Java supports pointer internally. But you can't write the pointer program in java. It means java has restricted pointer support in java.
▪️Compiler and Interpreter
C++ uses compiler only.
Java uses compiler and interpreter both.
▪️Call by Value and Call by reference
C++ supports both call by value and call by reference.
Java supports call by value only. There is no call by reference in java.
▪️Structure and Union
C++ supports structures and unions.
Java doesn't support structures and unions.
▪️Thread Support
C++ doesn't have built-in support for threads. It relies on third-party libraries for thread support. Java has built-in thread support.
▪️Documentation comment
C++ doesn't support documentation comment.
Java supports documentation comment (/** ... */) to create documentation for java source code.
▪️Virtual Keyword
C++ supports virtual keyword so that we can decide whether or not override a function.
Java has no virtual keyword. We can override all non-static methods by default. In other words, non-static methods are virtual by default.
▪️unsigned right shift >>>
C++ doesn't support >>> operator.
Java supports unsigned right shift >>> operator that fills zero at the top for the negative numbers. For positive numbers, it works same like >> operator.
▪️Inheritance Tree
C++ creates a new inheritance tree always.
Java uses single inheritance tree always because all classes are the child of Object class in java. Object class is the root of inheritance tree in java.
@javaCode☕️
#Java_Interview_Question
37) What is method overloading?
If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program.
@javaCode☕️
37) What is method overloading?
If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program.
@javaCode☕️
#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☕️
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☕️
#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☕️
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☕️
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☕️
#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☕️
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☕️
👉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☕️
#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☕️
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☕️
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☕️
#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☕️