Constructor Chaining in Java

Constructor chaining is the way through which a constructor make a call to another constructor. Constructor chaining can be done in 2 ways:

  1. Within Class (using constructor overloading) :- In this a constructor of the class calls another constructor of the same class. This is achieved by using the ‘this()’ keyword. Within the argument of ‘this()’ we pass the arguments of the constructor being called. ‘this()’ should be the first statement of the constructor. Let us understand with an example:
public class Tech4Humans {
	public static void main(String[] args) {

		System.out.println("Calling no parameter constructor");
		// Initializing object using no parameter constructor
		Example2 obj = new Example2();

		obj.printVariables();
	}

}

class Example2 {
	int age;
	String name;

	Example2() {
		// 'this' keyword is used for calling constructors within the class
		this(29);
		System.out.println("No parameter constructor completed");
	}

	Example2(int age) {
		this(age, "Robert");
		System.out.println("Single parameter constructor completed");
	}

	Example2(int age, String name) {
		this.name = name;
		System.out.println("Double parameter constructor completed");
	}

	void printVariables() {
		System.out.println("name = " + name);
		System.out.println("age = " + age);
	}
}

Output

Calling no parameter constructor
Double parameter constructor completed
Single parameter constructor completed
No parameter constructor completed
name = Robert
age = 0

In the above code we called Example2() constructor for object creation which in turn calls Example2(int age) constructor using this(29). Now inside Example2(int age) constructor a call is made to Example2(int age, String name) constructor using this(age, “Robert”).

  1. With Parent Class :– In this method of constructor chaining child class constructor calls the parent class constructor using ‘super()’ keyword. ‘super()’ should be the first statement of constructor. Within the argument of ‘super()’ we pass the arguments of the constructor being called. Let us understand with an example:
public class Tech4Humans {
	public static void main(String[] args) {
		Child obj = new Child("black", "blue", true);
		obj.printVariables();
	}

}

class GrandFather {
	String hairColor;

	GrandFather(String hairCol) {
		hairColor = hairCol;
		System.out.println("GrandFather constructor ended");
	}
}

class Father extends GrandFather {
	String eyeColor;

	Father(String hairCol, String eyeCol) {
		// 'super()' keyword is used for constructor chaining.
		super(hairCol);
		eyeColor = eyeCol;
		System.out.println("Father constructor ended");
	}
}

class Child extends Father {
	boolean contactLens;

	Child(String hairCol, String eyeCol, boolean lens) {
		// 'super()' keyword is used to call constructor of parent classes
		super(hairCol, eyeCol);
		contactLens = lens;
		System.out.println("Child constructor ended");
	}

	void printVariables() {
		System.out.println("Hair Color = " + hairColor);
		System.out.println("Eye Color = " + eyeColor);
		System.out.println("Usage of Contact Lens = " + contactLens);
	}
}
GrandFather constructor ended
Father constructor ended
Child constructor ended
Hair Color = black
Eye Color = blue
Usage of Contact Lens = true

In the above code a constructor is made by calling the contructor of Child class Child(“black”, “blue”, true). Inside the Child class constructor a call is made to parent class constructor Father(String hairCol, String eyeCol) using super(hairCol, eyeCol). In Father class constructor again a call is made to its parent class(GrandFather) constructor using super(hairCol).

Leave a Comment