# CONSTRUCTOR

&#x20;**Definition**: Constructor is a special method which is used to construct an object or create an object for a class. Constructor does not return any value or does not have any Return type. Whenever an object of class is created using new operator constructor will be executed.

&#x20;**Why Contructor?** We can use constructor to create object, without constructor we cannot create Object.&#x20;

**Rules to develop constructor:**&#x20;

* &#x20;Constructor named should be exactly same as Class name.&#x20;
* &#x20;Constructor should not have return type, should always be non-static should not return any value.&#x20;
* &#x20;Constructor is also called as Non-static initializer, which is used to initialize the object of a class. &#x20;
* No static and return type allowed.&#x20;
* &#x20;Constructor should be there for every class.&#x20;
* &#x20;If the programmer has not developed a constructor for class then there will be default constructor in that class.

&#x20;**Default Constructor:** The constructor developed by the compiler at compile-time is called as Default constructor. If a programmer has not developed a constructor for class then compiler will develop the default constructor.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfQBkgWpwZQhpMRyrP%2F-LTfQfYg3I-eE63RBhpo%2Fimage.png?alt=media\&token=37fb2789-217c-4feb-b43b-29ec0a3940b7)

> &#x20;Note: Steps in Object creation

* &#x20;New operator will allocate the new address space randomly in the heap memory.&#x20;
* &#x20;All the Non-Static members of class will be loaded onto the address space with default values.&#x20;
* Constructor body will be executed on Stack memory like a method. &#x20;
* Object address will be assigned to Reference variables.

#### &#x20;Why Programmer should write Constructor if java provides Default Constructor?&#x20;

If the programmer needs to initialize the instance variable then programmer can perform the initialization in Constructor. \
If the programmer wants to perform some start up activities whenever an object is created for the class, then programmer will reuse the Constructor body and all the startup code will be developed inside the Constructor.&#x20;

**Types of Constructor:**&#x20;

1\) Parameterized Constructor: The Constructor which is developed with some arguments. Example: A(int I)

&#x20;2\) No-Arguments Constructor: The Constructor which is developed without arguments. Example: A()

> &#x20;**Note:**  To create the object for a class we should utilize available Constructor of that class.&#x20;
>
> &#x20;If programmer develops a Constructor for class then java does not provide a default Constructor.

&#x20;**Constructor Overloading**: Developing multiple Constructors for a class with the variation in the Data-type, members and position of arguments is called Constructor overloading.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfQBkgWpwZQhpMRyrP%2F-LTfROlBW3fYR4huboQL%2Fimage.png?alt=media\&token=88f533dd-f6b4-4dee-b8dd-bdc45d8d6f15)

#### &#x20;Output:

&#x20;Main Starts&#x20;

Running Constructor

&#x20;Main Ends

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfQBkgWpwZQhpMRyrP%2F-LTfR_8qSp9LVagO-zpc%2Fimage.png?alt=media\&token=ac202692-d5b4-488e-9ca6-1d60bee23ae1)

#### &#x20;Output:

&#x20;Main Starts Running Demo1 constructor ------------ Running Demo1 constructor Main Ends

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfQBkgWpwZQhpMRyrP%2F-LTfRfk8M2t_C81LA4kT%2Fimage.png?alt=media\&token=bcf3bcf2-f182-4dfd-afb8-5f59735b1883)

#### &#x20;Output:

&#x20;Main Starts Main Ends

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfQBkgWpwZQhpMRyrP%2F-LTfRnwT06VRewEYcar0%2Fimage.png?alt=media\&token=3e17b33a-43bd-4071-b97a-7eed7112003a)

#### &#x20;Output:

&#x20;Main Starts Main Ends -----------

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfQBkgWpwZQhpMRyrP%2F-LTfRuK8xnG3DTJ7M1Tf%2Fimage.png?alt=media\&token=a3dc4a59-9796-4f22-96c5-20515b31a3dd)

#### &#x20;Can we Override constructor?

&#x20;NO, because constructor will not be inherited to sub class but for method overriding inheritance is mandatory.&#x20;

#### Can we override main method?

&#x20;NO, because main is static method, static method will not be inherited to sub class and for method overriding inheritance is mandatory.&#x20;

#### Can we Override Static members?

&#x20;NO, because static methods will not be inherited to sub class but for method overriding inheritance is mandatory.&#x20;

#### Q. What is constructor chaining in Java?&#x20;

Constructor chaining is a phenomenon of calling one constructor from another constructor of same class. Since constructor can only be called from another constructor in Java, constructor chaining is used for this purpose.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfQBkgWpwZQhpMRyrP%2F-LTfSKAWj09RwRn08iF_%2Fimage.png?alt=media\&token=7e3f70fc-1a70-40f5-9e05-3aa1572a2dac)

