☕️JAVA Language Community
More details: 👉Constructor in Java Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why…
#Java_Interview_Question
#Constructor
Constructor Overloading in Java
Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists.The compiler differentiates these constructors by taking into account the number of parameters in the list and their type.
@javaCode☕️
#Constructor
Constructor Overloading in Java
Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists.The compiler differentiates these constructors by taking into account the number of parameters in the list and their type.
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question #Constructor Constructor Overloading in Java Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists.The compiler differentiates these constructors by taking…
#Java_Interview_Question
#Constructor
Difference between constructor and method in java
There are many differences between constructors and methods. They are given below.
▫️Constructor is used to initialize the state of an object.
▪️Method is used to expose behaviour of an object.
▫️Constructor must not have return type.
▪️Method must have return type.
▫️Constructor is invoked implicitly.
▪️Method is invoked explicitly.
▫️The java compiler provides a default constructor if you don't have any constructor.
▪️Method is not provided by compiler in any case.
▫️Constructor name must be same as the class name.
▪️Method name may or may not be same as class name.
@javaCode☕️
#Constructor
Difference between constructor and method in java
There are many differences between constructors and methods. They are given below.
▫️Constructor is used to initialize the state of an object.
▪️Method is used to expose behaviour of an object.
▫️Constructor must not have return type.
▪️Method must have return type.
▫️Constructor is invoked implicitly.
▪️Method is invoked explicitly.
▫️The java compiler provides a default constructor if you don't have any constructor.
▪️Method is not provided by compiler in any case.
▫️Constructor name must be same as the class name.
▪️Method name may or may not be same as class name.
@javaCode☕️
#Java_Interview_Question
16) What is the purpose of default constructor?
The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class
@javaCode☕️
16) What is the purpose of default constructor?
The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class
@javaCode☕️
#Java_Interview_Question
17) Does constructor return any value?
Ans:yes, that is current instance (You cannot use return type yet it returns a value)
@javaCode☕️
17) Does constructor return any value?
Ans:yes, that is current instance (You cannot use return type yet it returns a value)
@javaCode☕️
#Java_Interview_Question
19) Can you make a constructor final?
No, constructor can't be final.
@javaCode☕️
19) Can you make a constructor final?
No, constructor can't be final.
@javaCode☕️
☕️JAVA Language Community
#Abstract_Factory_Design_Pattern
#Design_Patterns
#Abstract_Factory_Design_Pattern
❓Example
The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes. This pattern is found in the sheet metal stamping equipment used in the manufacture of Japanese automobiles. The stamping equipment is an Abstract Factory which creates auto body parts. The same machinery is used to stamp right hand doors, left hand doors, right front fenders, left front fenders, hoods, etc. for different models of cars. Through the use of rollers to change the stamping dies, the concrete classes produced by the machinery can be changed within three minutes.
@javaCode☕️
#Abstract_Factory_Design_Pattern
❓Example
The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes. This pattern is found in the sheet metal stamping equipment used in the manufacture of Japanese automobiles. The stamping equipment is an Abstract Factory which creates auto body parts. The same machinery is used to stamp right hand doors, left hand doors, right front fenders, left front fenders, hoods, etc. for different models of cars. Through the use of rollers to change the stamping dies, the concrete classes produced by the machinery can be changed within three minutes.
@javaCode☕️
#Java_Interview_Question
20) What is static variable?
static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
static variable gets memory only once in class area at the time of class loading.
@javaCode☕️
20) What is static variable?
static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
static variable gets memory only once in class area at the time of class loading.
@javaCode☕️
#Java_Interview_Question
📜More Details:
Java static keyword
▪️The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.
▪️The static can be:
variable (also known as class variable)
method (also known as class method)
block
nested class
▪️Advantage of static variable
It makes your program memory efficient (i.e it saves memory).
Java static property is shared to all objects.
@javaCode☕️
📜More Details:
Java static keyword
▪️The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.
▪️The static can be:
variable (also known as class variable)
method (also known as class method)
block
nested class
▪️Advantage of static variable
It makes your program memory efficient (i.e it saves memory).
Java static property is shared to all objects.
@javaCode☕️
#Design_Patterns
#Abstract_Factory_Design_Pattern
❓Check list
1️⃣Decide if "platform independence" and creation services are the current source of pain.
2️⃣Map out a matrix of "platforms" versus "products".
3️⃣Define a factory interface that consists of a factory method per product.
4️⃣Define a factory derived class for each platform that encapsulates all references to the new operator.
5️⃣The client should retire all references to new, and use the factory methods to create the product objects.
@javaCode☕️
#Abstract_Factory_Design_Pattern
❓Check list
1️⃣Decide if "platform independence" and creation services are the current source of pain.
2️⃣Map out a matrix of "platforms" versus "products".
3️⃣Define a factory interface that consists of a factory method per product.
4️⃣Define a factory derived class for each platform that encapsulates all references to the new operator.
5️⃣The client should retire all references to new, and use the factory methods to create the product objects.
@javaCode☕️
#Java_Interview_Question
21) What is static method?
A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.
@javaCode☕️
21) What is static method?
A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 21) What is static method? A static method belongs to the class rather than object of a class. A static method can be invoked without the need for creating an instance of a class. static method can access static data member and…
More details:
Restrictions for static method
There are two main restrictions for the static method.
They are:
1️⃣The static method can not use non static data member or call non-static method directly.
2️⃣this and super cannot be used in static context.
@javaCode☕️
Restrictions for static method
There are two main restrictions for the static method.
They are:
1️⃣The static method can not use non static data member or call non-static method directly.
2️⃣this and super cannot be used in static context.
@javaCode☕️
#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☕️