Constructor Chaining in Java

In this article, we will learn about constructor chaining in java. But before that, let us first see what is constructor in java. Let’s start!!!

What is a constructor in java?

A constructor is like a method that has the same name as the class. It is used to create objects or instances for a class. A constructor is automatically called when an object is created. It has no return type and cannot be final, abstract, static or synchronized.

Constructor Chaining in Java:

From the term “Constructor chaining,” we can understand that it is a type of continuation or sequence as a chain. This refers to the sequence of invoking constructors while an object is initialized. We can invoke the particular number of constructors one by one using just one instance using constructor chaining.

How does constructor chaining occur in java?

Usually, in a constructor chain, we can call a constructor from another constructor that belongs to the same class. This takes place via inheritance. This begins by creating an instance of the derived class. Firstly, the constructors of the inherited or the base class are invoked. Lastly, the constructor of the calling or derived class is invoked.

Why is java constructor chaining necessary?

It eliminates a lot of coding lines by processing multiple tasks through a single constructor. Therefore, we need not create a new set of coding for each constructor present in our program. This makes the program easily readable.

Rules of constructor chaining in java:

1. The keyword ‘this’ should be the starting line of the constructor.

2. Not every constructor should use the ‘this’ keyword; there should be at least one constructor left without it.

3. The order of the constructor chaining does not matter.

constructor chaining in java

Calling of java constructor:

The constructor call can be done in the following couple of ways:

  • Using this() keyword: We can use this option when we need to call the current class constructor within the same class.
  • Using super() keyword: This option works well when we need to call the superclass constructor from the base class.

And it is important to know that both this() and super() cannot be applied simultaneously.

Syntax for java chaining constructor:

this();    or 
this(parameters); // this(“FirstCode”);

Sample program to demonstrate constructor chaining in java:

public class FirstCode{
FirstCode(){
this(“Learn Java with FirstCode”);
System.out.println(“This is the default constructor”);
}
FirstCode(String s)
{
System.out.println(“This constructor has a parameter”);
}
public static void main(String args[]){
FirstCode fc = new FirstCode();
}
}

Output:

This constructor has a parameter
This is the default constructor

Explanation of the above program:

In this program, the class object is created without parameters. The default constructor is first called that is redirected to the parameterized constructor by this(). This makes the statements given inside the parameterized constructor execute first, followed by that present in the default constructor.

Here is a simple depiction of the calling sequence of the constructor:
FirstCode fc = new FirstCode(); -> FirstCode() -> FirstCode(String s) -> System.out.println() -> FirstCode() -> System.out.println()

Calling superclass constructor in java:

When we need to call the parent class constructor from the child class, we can use the super() keyword. A syntax error would occur when the superclass constructor is called in the child class. Same as this(), the super() keyword should be put in the first line. The super() keyword is only an optional one; the JVM automatically adds it is looking at the structure of the program.

Syntax:

  • super(); -> It calls the default constructor that has no arguments in the superclass.
  • super(parameters); -> It calls the parameterized constructor in the super class.

Sample program to demonstrate java constructor chaining using super():

class FirstCode{
FirstCode(){
this(23, 17);
System.out.println(“This belongs to the base class default constructor”);
}
FirstCode(int a, int b){
System.out.println(“This belongs to the base case class parameterized constructor”);
}
}
class FirstCode2 extends FirstCode{
FirstCode2(){
this(“Knowledge”, “Wisdom”);
System.out.println(“It belongs to the derived class default constructor”);
}
FirstCode2(String s1, String s2){
super();
System.out.println(“It belongs to the derived class parameterized constructor”);
}
}
public class ConsChaining{
public static void main(String args[]){
FirstCode2 fc2 = new FirstCode2();
}
}

Output:

This belongs to the base case class parameterized constructor
This belongs to the base class default constructor
It belongs to the derived case class parameterized constructor
It belongs to the derived class default constructor

Conclusion:

This article has explained constructor chaining in java in detail. You can work out the given sample programs to practically understand the working of this concept.

Leave a Reply

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