In Java, constructors play a crucial role in initializing the instance of the class. These are similar to the Java methods. However, the constructor name is always the same as the class name while a normal method can have any valid name. Mostly, these are also referred to as special methods to declare an object. You can create constructor chaining which occurs with the help of inheritance. The child/subclass constructor calls the parent/superclass constructor first and then, the child-class constructors can be invoked.
This post will state about the constructor chaining in Java.
Constructor Chaining in Java
The constructor changing is the most important procedure for calling a constructor into another constructor according to the stated objectives. One of the primary uses of constructor chaining is to avoid redundant codes while having multiple constructors. It makes the code easily understandable and readable.
There are two methods to perform constructor chaining in Java as mentioned below:
Let’s check out these methods one by one for a better understanding of constructor chaining.
Method 1: Constructor Chaining Within the Same Class Using “this()” Keyword
When the constructor chaining is performed within the same class, the Java “this()” keyword is used. The default constructor will be invoked when a class instance is initiated. It will call another constructor using the “this” keyword. Then, invoke the “println()” method for displaying the result on the console:
this(5);
System.out.println("The Default constructor");
}
Now, call the second constructor consisting of a parameter “a” and set the value of parameters in the “this()” keyword. Then, utilize the “println()” and pass “a” as the argument to show result:
this(5, 20);
System.out.println(a);
}
In this constructor, we have passed the two integer type parameters as “a” and “b”. After that, invoke the “println()” method and pass “a*b” which will return the product of these variables:
System.out.println(a*b);
}
In the “main()” method, invoke the default constructor that will automatically call the other constructors and display the result on the console:
new min();
}
As a result, the output of all constructors will be displayed on the console:
If you want to chain the constructor in multiple classes then, check out the below-stated method.
Method 2: Constructor Chaining to Another Class Using “super()” Keyword
You can also chain the constructors from one class to another. For this, the “super()” keyword is used. To do so, utilize the following code in the main class.
First of all, define a string type variable “name” and call the first constructor by using the main class name:
min() {
this("");
System.out.println("Without constructor of base class");
}
Invoke the second constructor and pass the above-declared variable “String name” as the parameter. Use the “this” keyword to access the value and invoke the “println()” method for printing purposes:
this.name = name;
System.out.println("Calling parameterized constructor of base");
}
Inside the “main()” method, call the child class with the parameter “name”. That will call the parent class constructors where the parameter “name” is passed. Then, it will invoke the child class constructor with the parameter “name”:
new Child("name");
}
A child class is created that uses the “extends” keyword to inherit the parent class and invoke the third constructor. After that call the next constructor and inside this constructor, invoke the parent class second constructor:
Child() {
System.out.println("Without argument constructor of child class");
}
Child(String name) {
super(name);
System.out.println("Calling parameterized constructor of child ");
}
}
Output
That’s all about constructor chaining in Java.
Conclusion
In Java, the constructor chaining in the same class is done using the “this()” keyword, while the “super()” keyword is used to perform constructor chaining on various classes. The constructor chaining occurs with the help of inheritance. The sub-class constructor calls the super-class constructor first and then, the child-class constructors can be invoked. This post has discussed the constructor chaining in Java with practical examples.