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

15) What is constructor?

Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.

@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 15) What is constructor? Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation. @javaCode☕️
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 it is known as constructor.

👉Rules for creating java constructor

There are basically two rules defined for the constructor.

Constructor name must be same as its class name
Constructor must have no explicit return type
Types of java constructors

👉There are two types of constructors:

1️⃣Default constructor (no-arg constructor)

A constructor that have no parameter is known as default constructor.

2️⃣Parameterized constructor

A constructor that have parameters is known as parameterized constructor.
Why use parameterized constructor?

Parameterized constructor is used to provide different values to the distinct objects.

@javaCode☕️
#Design_Patterns

#Abstract_Factory_Design_Pattern

Intent

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
A hierarchy that encapsulates: many possible "platforms", and the construction of a suite of "products".
The new operator considered harmful.

Problem

If an application is to be portable, it needs to encapsulate platform dependencies. These "platforms" might include: windowing system, operating system, database, etc. Too often, this encapsulation is not engineered in advance, and lots of #ifdef case statements with options for all currently supported platforms begin to procreate like rabbits throughout the code.

🤓To be continued tomorrow...

@javaCode☕️
#Design_Patterns

#Abstract_Factory_Design_Pattern

Discussion

Provide a level of indirection that abstracts the creation of families of related or dependent objects without directly specifying their concrete classes. The "factory" object has the responsibility for providing creation services for the entire platform family. Clients never create platform objects directly, they ask the factory to do that for them.

This mechanism makes exchanging product families easy because the specific class of the factory object appears only once in the application - where it is instantiated. The application can wholesale replace the entire family of products simply by instantiating a different concrete instance of the abstract factory.

Because the service provided by the factory object is so pervasive, it is routinely implemented as a Singleton.

@javaCode☕️
#Design_Patterns

#Abstract_Factory_Design_Pattern

Structure

The Abstract Factory defines a Factory Method per product. Each Factory Method encapsulates the new operator and the concrete, platform-specific, product classes. Each "platform" is then modeled with a Factory derived class.

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

18)Is constructor inherited?

No, constructor is not inherited.

@javaCode☕️
#Java_Interview_Question

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