What is constructor chaining in Java?
In Java, constructor chaining is a sequence of invoking constructors upon initializing an object. It is used when we want to invoke a number of constructors, one after another by using only an instance. In this section, we will discuss constructor chaining in Java in detail with proper examples. Let's have a quick look at what is a constructor in Java.
In Java, a constructor is the same as a method but the only difference is that the constructor has the same name as the class name. It is used to create an instance of the class. It is called automatically when we create an object of the class. It has no return type. Remember that a constructor cannot be abstract, final, synchronized, and static. We cannot override a constructor.
There are two types of constructor in Java:
In constructor chain, a constructor is called from another constructor in the same class this process is known as constructor chaining. It occurs through inheritance. When we create an instance of a derived class, all the constructors of the inherited class (base class) are first invoked, after that the constructor of the calling class (derived class) is invoked.
We can achieve constructor chaining in two ways:
Remember that changing the order of the constructor does not affect the output.
The Need of Constructor Chaining
Suppose, there are five tasks to perform. There are two ways to perform these tasks, either implement all the tasks in a single constructor or create separate tasks in a single constructor.
By using the constructor chaining mechanism, we can implement multiple tasks in a single constructor. So, whenever we face such types of problems, we should use constructor chaining. We can make the program more readable and understandable by using constructor chaining.
Rules of Constructor Chaining
Constructor Calling form another Constructor
The calling of the constructor can be done in two ways:
Note: In the same constructor block, we cannot use this() and super() simultaneously.
Let's understand these two concepts through Java programs.
Constructor Chaining Examples
Calling Current Class Constructor
We use this() keyword if we want to call the current class constructor within the same class. The use of this() is mandatory because JVM never put it automatically like the super() keyword. Note that this() must be the first line of the constructor. There must exist at least one constructor without this() keyword.
Let's create a Java program and call the current class constructor.
In the above example, we have created an instance of the class without passing any parameter. It first calls the default constructor and the default constructor redirects the call to the parameterized one because of this(). The statements inside the parameterized constructor are executed and return back to the default constructor. After that, the rest of the statements in the default constructor is executed and the object is successfully initialized. The following is the calling sequence of the constructor:
Calling Super Class Constructor
Sometimes, we need to call the superclass (parent class) constructor from the child class (derived class) in such cases, we use the super() keyword in the derived class constructor. It is optional to write super() because JVM automatically puts it. It should always write in the first line. We get a syntax error if we try to call a superclass constructor in the child class.
super(): It calls the no-argument or default constructor of the superclass.
super(parameters): It invokes the superclass parameterized constructor.
Remember that the superclass constructor cannot be inherited in the subclass. It can be called from the subclass constructor by using the super keyword.
Let's create a Java program and implement constructor chaining in an inherited class.