While programming in Java, there can be instances where the developer needs to integrate various code functionalities. For instance, linking the associated or interdependent features with minimal lines of code. In such situations, the “chain constructors” in Java assist in automating the code functionalities by just creating a class object, thereby streamlining the code complexity.
This article will elaborate on the approaches to “chain constructors” in Java.
How to “Chain Constructors” in Java?
The methodology of accessing a set of constructors upon initialization/creation of a class object is referred to as “constructor chaining”. Constructor chaining is helpful when there is a need to access multiple constructors, one after another.
This can be achieved with the help of “this()” and “super()” methods. The former method invokes the calling class constructor and the latter method accesses the inherited class constructor.
Example 1: Chaining Constructors in a Single Class in Java
In this example, the constructors can be chained in the same class. This can be done by using the “this()” method that accesses the parameterized constructor and display its functionalities first:
Chain(){
this("Java Programming!");
System.out.println("This is default constructor!");
}
Chain(String x){
System.out.println("This is constructor with parameters!");
}
}
public class chainconstructor {
public static void main( String args[] ) {
Chain object = new Chain();
}}
In the above code snippet:
-
- Firstly, define a class named “Chain”.
- In its definition, include the default class constructor redirecting to the chained parameterized constructor via the “this()” method and display the stated message.
- Note that the passed string argument in the “this()” method identifies and invokes the chained constructor.
- Now, define the constructor with a parameter accumulating the “String” data type containing the provided message.
- In the “main”, create an object of the class named “object” utilizing the “new” keyword and the “Chain()” constructor, respectively.
- Algorithm: The code executes in such a manner that the created object points to the default constructor and this constructor invokes the chained constructor via the “this()” method and displays its(parameterized constructor) functionalities first and then reverts back to its(default) own.
Output
In the above output, it can be observed that the redirected chained constructor (parameterized) is invoked prior to the default constructor.
Example 2: Chaining Constructors in Inherited Class in Java
In this particular example, the constructors can be chained via the “inherited” class:
ChainParent(){
this("Java Programming!");
System.out.println("This is parent default constructor!");
}
ChainParent(String x){
System.out.println("This is parent constructor with parameters!");
}}
class ChainChild extends ChainParent{
ChainChild(){
this("Linuxhint!");
System.out.println("This is child default constructor!");
}
ChainChild(String x){
super();
System.out.println("This is child constructor with parameters!");
}}
public class chainconstructor2 {
public static void main( String args[] ) {
ChainChild object = new ChainChild();
}}
In this code block:
-
- Likewise, define a parent class named “ChainParent” containing the former constructor invoking the parameterized constructor using the “this()” method and the passed argument.
- Now, declare the child class “ChainChild” inheriting the parent class with the help of the “extends” keyword.
- In this class, repeat the discussed approaches for including the default and parameterized constructors and redirecting to the latter constructor via the “this()” method.
- In the parameterized constructor, use the “super()” method to invoke the default constructor of the inherited class.
- In the “main()” method, create an object of the inheriting(child) class via the discussed approach.
- Sequence of Execution: Parent Class Parameterized Constructor-> Parent Class Default Constructor-> Child Class Parameterized Constructor-> Child Class Default Constructor.
Output
In this outcome, it can be analyzed that the chaining is done perfectly.
Conclusion
The constructors in Java can be chained with the help of “this()” and “super()” methods by invoking the constructor of the calling class and the constructor of the inherited class, respectively. The former method chains the constructors within the same class whereas the latter method applies chaining via the inherited class, respectively. This blog stated the approaches to chain constructors in Java.