What is constructor chaining in java?

Preetam G K
3 min readOct 29, 2023

--

Photo by AltumCode on Unsplash

In this article , We will learn what is constructor chaining in java →directly jumping into the topic. It is simple (one constructor calling another constructor within the same class or from parent class.)

Constructor chaining can be achieved in the two ways :

  • Within the same class
  • Parent class

In order to achieve constructor chaining within the same class we have to perform the constructor overloading and by using this().

Note : There is a difference between this keyword and this() calling statements. this → It is a keyword and it is used to invoke the methods and the variables that are present in the same class. this() →It is not a keyword and it is used to invoke the overloaded constructor in the same class.

The program illustrates constructor chaining within the same class :

class Demo{
Demo(){
this(2);
System.out.println("Constructor 1");
}
Demo(int x){
this(x,1);
System.out.println("Constructor "+x);
}
Demo(int x,int y){
System.out.println("Constructor "+(x+y));
}
public static void main(String args[]){
new Demo();
}
}
Output : 
Constructor 3
Constructor 2
Constructor 1

Explanation : When the object is created it calls the zero parameter constructor in which it has this(2) calling statement which call the constructor having a single parameter because we are passing only one argument. And in that constructor we have one more this(x,1) calling statements which call the constructor having two parameters.

So it prints the constructor 3 first and the execution line goes back to the line where it invoke the the two parameter constructor and after that it print Constructor 2 then again the execution line goes back to the line where it invoke the single parameter constructor. and then prints the Constructor 1.

Now let us know about how to achieve the constructor chaining from parent class:

In order to achieve the constructor chaining from parent class .It should be in the IS-A relation(inheritance) and by using the super() calling statements.

Note : There is also a difference between the super keyword and super() calling statements. super→ super is a keyword and it is used to invoke the superclass variables and methods. super() → super() is not a keyword and it is used to invoke the superclass constructor.

The program illustrates constructor chaining from parent class :

class Father{
Father(){
System.out.println("This is father's class");
}
}
public class Son extends Father {
Son(){
System.out.println("This is son's class");
}
public static void main(String[] args) {
new Son();
}
}
Output : 
This is father’s class
This is son’s class

Now you might be wondering why father’s class constructor is executed first and then son’s class constructor is executed second as we have not specified any super() calling statement in the son’s class constructor.

Because in Java the super() is implicitly invoked in the child class constructor.so only when the son class object gets created the constructor gets executed of the son class where it has super() calling statement implicitly so it invoke the parent class constructor nothing but the father class constructor.

That’s all for this article .Hope you have enjoyed this article : )

Thank you.

--

--