Beginners Programs :

Java Constructor Chaining – Types & Example | Tutorials

Java Constructor Chaining – Calling one constructor from another constructor is generally referred to as constructor chaining. This can be achieved in two ways.

  1. this()
  2. super()

in some cases we use only this(), in some other cases, we use only super(). Sometimes we use both (but not directly).

Normal Java constructor calling (without chaining)

When we create an object, its constructor is invoked and the code in that constructor is executed. By default, the default constructor (a constructor without arguments) is invoked when we create an object.

If we specify any arguments along with object creation, then the corresponding (argumented) constructor will be executed.

Example 1:

Example 2:

Output:

Constructor chaining with this()

Constructor calling (with chaining):

Suppose we want to call both the constructors (the default constructor and the argumented constructor) with an object creation, then we call one constructor from another constructor so that both will be executed.

This mechanism of invoking more than one constructor (one constructor calling another constructor) with a single object creation is known as constructor chaining.

The method this() is used to call one constructor from another constructor.

Example for this():

Output:  

In the above example, when an object is created, the control goes to the argumented constructor first (as the object is created with an int argument) and found this() as the first statement.

With this(), control goes to the default constructor and executes the code in that (prints A) and then comes back to execute the code in the argumented constructor (prints B).

More constructors involved in chaining

If we want to call the default constructor then this() is used without any arguments. If we want to call any argumented constructor then this() is used with corresponding arguments.

In the following example, we have 3 constructors involved in the chaining.

Example:

Output:

In the above example, we are creating an object without arguments. So the default constructor is invoked first. From there (default constructor) the method this() is used with two arguments like “this(10,20)”.

  • So the corresponding constructor (with 2 arguments) will be invoked and control goes there. In that constructor (2-args) again this() is used with one argument like “this(x)”.
  • So the control goes to the 1-argumented constructor of the class to print B. From there it will come back to 2-args constructor to print C. from there it will come back to default constructor to print A.
Recursive constructor invocation is not allowed

We can call any number of constructors in this way. But an already invoked constructor should not be called again in the sequence. It means recursion is not allowed in constructor chaining. so the following code is invalid.

Example:

The this() should be the first statement in a constructor if it exists. We cannot write this() after any statement. So the following code is invalid.

Example:

Constructor Chaining with super()

We can call a constructor of the current class only with this(). When inheritance is involved, and a child class object is created, an object of the parent class is also created as part of the child class object.

  • As parent object is created (along with child object) its constructor should also be invoked. In this case also, by default, the default constructor of the parent class is invoked with child class constructor.
  • If we want any argumented constructor of the parent class to be called with a child class constructor then we can exclusively invoke that using super().
  • Using super() without arguments will call the default constructor of the parent class.
  • To call (invoke) any argumented constructor of the parent class we should invoke super() with corresponding arguments.

Note: when a child class object is created, even though the control comes to child class constructor first, it will not execute the code in child class constructor.

From child class constructor it will go to the parent class constructor and executes it. then only it comes back to execute the child class constructor. So the parent class constructor is executed before the child class constructor.

Example:

Output:

In the above example, we have created 3 objects of Emp (child class). For each object creation, a parent class constructor and a child class constructor are invoked.

For the first statement, “new Emp()”, control goes to default constructor of child class (Emp) and there is no specification of which version of the parent class constructor to call.

So the default constructor of the parent class (Person) is invoked. After executing the parent class constructor the control comes back to execute the actual code of child class constructor. This part prints AX.

  • For the second statement, “new Emp(10)”, control goes to the 1-argumented constructor of Emp and found super() without arguments there.
  • So in this case also the default constructor of Person is executed and then, after coming back, the code in Emp(int) is executed. This part prints AY.
  • For the third statement, “new Emp(20,30)”, control goes to the 2-argumented constructor of Emp and found super(x). so it will go to the 1-argumented constructor of Person.

Hence code in Person(int) is executed and then, after coming back, the code in Emp(int, int) is executed. This part prints BZ.

More chaining with super():

Suppose three classes are involved (say A, B and C where A is a parent to B and B is a parent to C) and third class (C) object is created, then control goes to the constructor of C

But before executing the statements in that it goes to the constructor of B. Even in B, before executing statements of B, it goes to the constructor of A.

After executing the code in A constructor, the control comes back to execute the code in B’s constructor. Once that execution is over, the control comes to execute code in C.

Example:

Output:

super() should be the first statement

similar to this(), the super() should also be the first statement in a constructor, if it exists. So the following code is invalid.

Example:

A constructor cannot have both this() and super() at the same time

As this() and super() should be the first statement(s) in a constructor we cannot use them in a single constructor at a time. So this following code will not be executed.

Example:

this() and super() both involved:

Even though the system does not allow this() and super() together used in a single constructor, then can be used in constructor chaining so that they get executed accordingly.

Example:

Output:    

In the above example, first control goes to the argumented constructor of B and from there it will go to default constructor of B (with this()). From there, it will go to the argumented constructor of A (with super(10)).

From there it will move to default constructor of A (with this()). From there the printing takes place in chaining.

Note: if we have a constructor without a super() or this(), then the system automatically assumes super() as existing. So the following two codes are the same.

Example 1:

Example 2: