numeric data types are set to 0

	char data types are set to null character(‘\0’)

	reference variables are set to null

In this tutorial, you will learn-

Rules for creating a Constructor Constructor Overloading in Java Constructor Chaining

Rules for creating a Java Constructor

	It has the same name as the class

	It should not return a value not even void

Example 1: Create your First Constructor in Java Step 1) Type the following constructor program in Java editor. Step 2) Save, Run & Compile the constructor program in Java and observe the output. Output:

Constructor Overloading in Java

Java Constructor overloading is a technique in which a class can have any number of constructors that differ in parameter list. The compiler differentiates these constructors by taking into account the number of parameters in the list and their type. Examples of valid constructors for class Account are Example 2: To understand Constructor Overloading in Java Step 1) Type the code in the editor. Step 2) Save, Compile & Run the Code. Step 3) Error = ?. Try and debug the error before proceeding to next step of Java constructor overloading Step 4) Every class has a default Constructor in Java. Default overloaded constructor Java for class Demo is Demo(). In case you do not provide this constructor the compiler creates it for you and initializes the variables to default values. You may choose to override this default constructor and initialize variables to your desired values as shown in Example 1. But if you specify a parametrized constructor like Demo(int a), and want to use the default constructor Java Demo(), it is mandatory for you to specify it. In other words, in case your overloading constructor in Java is overridden, and you want to use the default constructor Java, its need to be specified. Step 5) Uncomment line # 4-8. Save, Compile & Run the code.

Constructor Chaining

Consider a scenario where a base class is extended by a child. Whenever an object of the child class is created, the constructor of the parent class is invoked first. This is called Constructor chaining. Example 3: To understand constructor chaining Step 1) Copy the following code into the editor. Step 2) Run the Code. Owing to constructor chaining, when the object of child class DemoChild is created, constructor Demo() of the parent class is invoked first and later constructor DemoChild() of the child is created. Expected Output = Step 3) You may observe the constructor of the parent class Demo is overridden. What if you want to call the overridden constructor Demo(int a) instead of the default constructor Demo() when your child object is created? In such cases, you can use the keyword “super” to call overridden constructors of the parent class. Syntax:- Example: If your constructor is like Demo(String Name,int a), you will specify super(“Java”,5). If used, the keyword super needs to be the first line of code in the constructor of the child class. Step 4) Uncomment Line # 26 and run the code. Observe the Output. Output: