Constructor Chaining Program in Java

What is Constructor Chaining in Java?
Constructor chaining in Java is the process of calling one constructor from another constructor within the same class or from the parent class.

In Java, constructor chaining is achieved using the this() keyword, which is used to call another constructor within the same class, and the super() keyword, which is used to call a constructor in the parent class.

Problem Description

Write a Java Program to Illustrates the Use of Chaining Constructor.

What is the purpose of constructor chaining?
Constructor chaining is a technique in Java that allows us to reuse the same piece of code across multiple constructors. This helps us initialize objects in a consistent way, gives users more options, and makes it easier to maintain the code. By using constructor chaining, we can simplify the code and reduce duplication, which makes it easier to write and maintain Java programs.

Rules of Constructor Chaining

  • Constructor chaining must be performed in the first line of a constructor.
  • No recursive calls.
  • When chaining to another constructor in the same class using this(), it must be a non-static constructor.
  • When chaining to a constructor in the parent class using super(), it must be the first statement in the constructor.
  • Default constructor is added if no call.
Problem Solution

Here, we use this() to achieve Constructor Chaining and there must be atleast one constructor which is not having this() as a first line. Constructor Chaining can be achieved in any order.

Program/Source Code

Here is the source code of the Java Program to Illustrates Use of Chaining Constructor. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

 * Constructor Chaining Program in Java using this()
public class ConstructorChaining
        System.out.println("Default constructor of class.");
    ConstructorChaining(int x)
        System.out.println("Parameterized (1 parameter) constructor of class.");
        System.out.println("The value of x is "+x);
    ConstructorChaining(int x, int y)
        System.out.println("Parameterized (2 parameters) constructor of class.");
        System.out.println("The  value of x and y is  " + x + "and " + y + ". " 
                            + "The sum of x and y is " + (x + y) );
    public static void main(String... a)
Program Explanation

1. A class named ConstructorChaining is defined with three constructors.
2. The first constructor is the default constructor, which calls the second constructor with the argument value of 20 using this(20).
3. The second constructor is a parameterized constructor that takes one integer argument. It prints the value of x and is called from the default constructor using this(20).
4. The third constructor is also a parameterized constructor that takes two integer arguments. It calls the default constructor using this() and prints the value of x and y as well as their sum.
5. The main method calls the third constructor with arguments 11 and 12, which in turn calls the default constructor and prints the desired output.

Program Output
$ javac
$ java ConstructorChaining
Parameterized (1 parameter) constructor of class.
The value of x is 20
Default constructor of class.
Parameterized (2 parameters) constructor of class.
The  value of x and y is  11and 12. The sum of x and y is 23

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.