#Java_Interview_Question
22) Why main method is static?
because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation.
@javaCode☕️
22) Why main method is static?
because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation.
@javaCode☕️
#Design_Patterns
#Abstract_Factory_Design_Pattern
👉Rules of thumb
▪️Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory could be used profitably. At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects, Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementation.
▪️Abstract Factory, Builder, and Prototype define a factory object that's responsible for knowing and creating the class of product objects, and make it a parameter of the system. Abstract Factory has the factory object producing objects of several classes. Builder has the factory object building a complex product incrementally using a correspondingly complex protocol. Prototype has the factory object (aka prototype) building a product by copying a prototype object.
▪️Abstract Factory classes are often implemented with Factory Methods, but they can also be implemented using Prototype.
▪️Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
▪️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.
▪️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☕️
#Abstract_Factory_Design_Pattern
👉Rules of thumb
▪️Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory could be used profitably. At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects, Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementation.
▪️Abstract Factory, Builder, and Prototype define a factory object that's responsible for knowing and creating the class of product objects, and make it a parameter of the system. Abstract Factory has the factory object producing objects of several classes. Builder has the factory object building a complex product incrementally using a correspondingly complex protocol. Prototype has the factory object (aka prototype) building a product by copying a prototype object.
▪️Abstract Factory classes are often implemented with Factory Methods, but they can also be implemented using Prototype.
▪️Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
▪️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.
▪️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
23) What is static block?
Is used to initialize the static data member.
It is excuted before main method at the time of classloading.
@javaCode☕️
23) What is static block?
Is used to initialize the static data member.
It is excuted before main method at the time of classloading.
@javaCode☕️
#Java_Interview_Question
24) Can we execute a program without main() method?
Ans) Yes, one of the way is static block.
@javaCode☕️
24) Can we execute a program without main() method?
Ans) Yes, one of the way is static block.
@javaCode☕️
#Java_Interview_Question
25) What if the static modifier is removed from the signature of the main method?
Program compiles. But at runtime throws an error "NoSuchMethodError".
@javaCode☕️
25) What if the static modifier is removed from the signature of the main method?
Program compiles. But at runtime throws an error "NoSuchMethodError".
@javaCode☕️
#Design_Patterns
#Builder_Design_Pattern
👉Intent
▪️Separate the construction of a complex object from its representation so that the same construction process can create different representations.
▪️Parse a complex representation, create one of several targets.
@javaCode☕️
#Builder_Design_Pattern
👉Intent
▪️Separate the construction of a complex object from its representation so that the same construction process can create different representations.
▪️Parse a complex representation, create one of several targets.
@javaCode☕️
#Design_Patterns
#Builder_Design_Pattern
👉Problem
An application needs to create the elements of a complex aggregate. The specification for the aggregate exists on secondary storage and one of many representations needs to be built in primary storage.
@javaCode☕️
#Builder_Design_Pattern
👉Problem
An application needs to create the elements of a complex aggregate. The specification for the aggregate exists on secondary storage and one of many representations needs to be built in primary storage.
@javaCode☕️
#Java_Interview_Question
26) What is difference between static (class) method and instance method?
1️⃣A method i.e. declared as static is known as static method. A method i.e. not declared as static is known as instance method.
2️⃣Object is not required to call static method. Object is required to call instance methods.
3️⃣Non-static (instance) members cannot be accessed in static context (static method, static block and static nested class) directly. static and non-static variables both can be accessed in instance methods.
4️⃣For example: public static int cube(int n){ return n*n*n;} For example: public void msg(){...}.
@javaCode☕️
26) What is difference between static (class) method and instance method?
1️⃣A method i.e. declared as static is known as static method. A method i.e. not declared as static is known as instance method.
2️⃣Object is not required to call static method. Object is required to call instance methods.
3️⃣Non-static (instance) members cannot be accessed in static context (static method, static block and static nested class) directly. static and non-static variables both can be accessed in instance methods.
4️⃣For example: public static int cube(int n){ return n*n*n;} For example: public void msg(){...}.
@javaCode☕️
#Java_Interview_Question
27) What is this in java?
It is a keyword that that refers to the current object.
@javaCode☕️
27) What is this in java?
It is a keyword that that refers to the current object.
@javaCode☕️
#Java_Interview_Question
More Details:
👉Usage of java this keyword
Here is given the 6 usage of java this keyword.
1️⃣this can be used to refer current class instance variable
2️⃣this can be used to invoke current class method (implicitly)
3️⃣this() can be used to invoke current class constructor.
4️⃣this can be passed as an argument in the method call.
5️⃣this can be passed as argument in the constructor call.
6️⃣this can be used to return the current class instance from the method.
▪️Suggestion: If you are beginner to java, lookup only three usage of this keyword.
@javaCode☕️
More Details:
👉Usage of java this keyword
Here is given the 6 usage of java this keyword.
1️⃣this can be used to refer current class instance variable
2️⃣this can be used to invoke current class method (implicitly)
3️⃣this() can be used to invoke current class constructor.
4️⃣this can be passed as an argument in the method call.
5️⃣this can be passed as argument in the constructor call.
6️⃣this can be used to return the current class instance from the method.
▪️Suggestion: If you are beginner to java, lookup only three usage of this keyword.
@javaCode☕️
#Design_Patterns
#Builder_Design_Pattern
👉Discussion
Separate the algorithm for interpreting (i.e. reading and parsing) a stored persistence mechanism (e.g. RTF files) from the algorithm for building and representing one of many target products (e.g. ASCII, TeX, text widget). The focus/distinction is on creating complex aggregates.
The "director" invokes "builder" services as it interprets the external format. The "builder" creates part of the complex object each time it is called and maintains all intermediate state. When the product is finished, the client retrieves the result from the "builder".
Affords finer control over the construction process. Unlike creational patterns that construct products in one shot, the Builder pattern constructs the product step by step under the control of the "director".
@javaCode☕️
#Builder_Design_Pattern
👉Discussion
Separate the algorithm for interpreting (i.e. reading and parsing) a stored persistence mechanism (e.g. RTF files) from the algorithm for building and representing one of many target products (e.g. ASCII, TeX, text widget). The focus/distinction is on creating complex aggregates.
The "director" invokes "builder" services as it interprets the external format. The "builder" creates part of the complex object each time it is called and maintains all intermediate state. When the product is finished, the client retrieves the result from the "builder".
Affords finer control over the construction process. Unlike creational patterns that construct products in one shot, the Builder pattern constructs the product step by step under the control of the "director".
@javaCode☕️
#Java_Interview_Question
28)What is Inheritance?
Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.
@javaCode☕️
28)What is Inheritance?
Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.
@javaCode☕️
👍1
#Java_Interview_Question
More Details:
👉Inheritance in Java
▪️Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
▪️The idea behind inheritance in java is that you can create new classes that are built upon existing classes.
▪️When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
▪️Inheritance represents the IS-A relationship, also known as parent-child relationship.
👉Why use inheritance in java
▪️For Method Overriding (so runtime polymorphism can be achieved).
▪️For Code Reusability.
@javaCode☕️
More Details:
👉Inheritance in Java
▪️Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
▪️The idea behind inheritance in java is that you can create new classes that are built upon existing classes.
▪️When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
▪️Inheritance represents the IS-A relationship, also known as parent-child relationship.
👉Why use inheritance in java
▪️For Method Overriding (so runtime polymorphism can be achieved).
▪️For Code Reusability.
@javaCode☕️
👍1
#Java_Interview_Question
30) Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.
@javaCode☕️
30) Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.
@javaCode☕️
#Java_Interview_Question
31) What is composition?
Holding the reference of the other class within some other class is known as composition.
@javaCode☕️
31) What is composition?
Holding the reference of the other class within some other class is known as composition.
@javaCode☕️
#Java_Interview_Question
32) What is difference between aggregation and composition?
Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike has an indicator (aggregation) but bike has an engine (compostion).
@javaCode☕️
32) What is difference between aggregation and composition?
Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike has an indicator (aggregation) but bike has an engine (compostion).
@javaCode☕️
#Design_Patterns
#Builder_Design_Pattern
👉Structure
The Reader encapsulates the parsing of the common input. The Builder hierarchy makes possible the polymorphic creation of many peculiar representations or targets.
@javaCode☕️
#Builder_Design_Pattern
👉Structure
The Reader encapsulates the parsing of the common input. The Builder hierarchy makes possible the polymorphic creation of many peculiar representations or targets.
@javaCode☕️
#Java_Interview_Question
33) Why Java does not support pointers?
Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.
@javaCode☕️
33) Why Java does not support pointers?
Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.
@javaCode☕️