Constructor Chaining in Java

Constructor chaining in Java is the process of calling one constructor from another constructor with in the same class or calling the parent class constructor from the child class.

So constructor chaining in Java can be done in two ways-

  1. When calling one constructor from another with in the same class. In this case this Keyword can be used to call constructors in chain.
  2. In case of inheritance when calling the parent class’ constructor from the child class. In this case super keyword can be used to call constructors.

How does constructor chaining help

Constructor chaining in Java helps with reducing the code redundancy by doing the task of initialization in one of the constructor. All the other constructors merely call that constructor in a chain for initialization.

Consider the scenario where you have 3 fields in your class and you want to give option to initialize all of them or two of them or only a single one or none. If you keep the initialization in all of the constructors then the code will look like as shown below.

Code without constructor chaining

As you can see there is redundancy of initialization code with in the constructors. By using constructor chaining where one constructor calls another same code can be written as following.

Code with constructor chaining

Output

As you see now the initialization is done by the single constructor in the class, all the other constructors just call that constructor in a chain rather than doing the initialization themselves.

Constructor chaining in Java with inheritance

Constructor chaining in the case of inheritance is the process of calling the parent class constructor from the child class. Rather than initializing the fields of the parent class again, in the constructor of the child class you can call the constructor of the parent class using super keyword. This helps in reducing code duplication.

Output

In this example you can see that the parent class constructor is called from the child class constructor using the super keyword to initialize the fields of the parent class.

Rules regarding constructor chaining with super

  1. If super() is used in a constructor to call the constructor of the parent class then it has to be the first statement in the constructor otherwise you will get compile time error “Constructor call must be the first statement in a constructor”.
  2. If you don’t call the parent class constructor explicitly then the default no-arg constructor of each super class will be executed implicitly.
  3. In case of constructor chaining from child class to parent class (inheritance hierarchy) the order of calling the constructor is from parent class to child class.

Let’s see it with an example.

In this code A is the super class which is extended by class B which in turn is extended by class C.

When you create an object of class C, super class constructors will be called in chain starting from super class to child class giving the output as following.

Also note that from the constructor of class B, constructor of class A is not called explicitly still it is called because the default no-arg constructor of the superclass will be executed implicitly.

That’s all for the topic Constructor Chaining in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.