What is Constructor Chaining in Java – 2024 Guide

  • Home
  • Blog
  • What is Constructor Chaining in Java – 2024 Guide

What is Constructor Chaining in Java: 2024 Guide

Welcome to our comprehensive blog on What is Constructor Chaining in Java: 2024 Guide. If you’ve ever delved into the world of Java programming, you’ve likely encountered the term ‘constructor chaining.’ But what exactly does it mean, and why is it so essential for Java developers? Let’s break all of this down together and understand better.

Constructor chaining is a sequence of invoking constructors upon initializing an object. It is used when we want to invoke many constructors, one after another by using only an instance.

 

What is Constructor Chaining in Java?

  • Constructor chaining in Java is a process of calling one constructor from another constructor within the same class or different class.
  • It occurs through Inheritance.
  • When we create an instance of a derived class, first, all the constructors of the Base class are invoked, and then the constructor of the derived class is invoked.

 

How to Implement Constructor Chaining in Java?

1) Constructor chaining in Java using the “this ()” Keyword.

2) Constructor chaining in Java using the “Super()” Keyword.

 

Diagram 1:

Constructor Chaining in the same class - 1

 

 

 

 

 

 

 

 

 

 

 

 

Diagram 2:

Constructor Chaining in the same class - 2

 

 

 

 

 

 

 

 

 

 

 

 

What are the rules of Constructor Chaining in Java?

Some of the rules that need to be followed for Constructor Chaining in Java are:

  • this() keyword can call the same class constructor only.
  • super() keyword can call immediate super class constructor only.
  • this() and super() must be the first statement in the constructor.
  • this() and super() cannot be used in the same constructor, because both must be a first statement in the constructor and that is not possible.
  • If this() or super() is not added in a constructor then the Java compiler implicitly adds a super() statement in the constructor that will call the immediate superclass default or no-argument constructor.

 

Constructor chaining in Java using “this()” Keyword

If we want to chain constructors present within the same class “this” keyword is used. The below example demonstrates the constructor chaining in Java using “this” keyword. The class has two constructors – one that takes no arguments and one that takes a String parameter. The constructor with no arguments calls the constructor with a String parameter using “this” keyword.

 

Code Example:

Constructor Chaining within the same class:-

// Constructor chaining within the same class

public class Employee {

// Default constructor

Employee() {

 

// Constructor Chaining Within the Same Class

this(2023);

System. out.println(“In Default Constructor Of Employee Class”);

}

 

// Parameterized constructor with Integer Parameter

Employee (int employeeId) {

 

// Constructor Chaining Within the Same Class

this(“Ankush”);

   System.out.println(“In Parameterized Constructor with Integer Parameter: ” + employeeId);

}

 

// Parameterized constructor with String Parameter

Employee (String Ankush) {

System.out.println(“In Parameterized constructor With String Parameter: ” + employeeName);

}

 

public static void main(String arg[]) {

Employee employeeObj = new Employee();

}

}

 

Output:

In Parameterized constructor With String Parameter: Ankush.

In Parameterized constructor With Integer Parameter: 2023

In Default Constructor Of Employee Class.

 

Code Explanation:

The main method is the entry point of the program. Inside the main method, an object employee-obj of type Employee is created by invoking new Employee();

The Employee constructor is invoked due to a new Employee();

It then calls this(2023), invoking the Employee(int employeeId) constructor.

This constructor is called with the value 2023.

Within this constructor, it calls this(“Ankush”); invoking the Employee(String employeeName) constructor.

This constructor is called with the string “Ankush”.

Inside this constructor, it prints the message In the Parameterized constructor With String Parameter: Ankush.

After the call to this(“Ankush”); it continues executing the next line, printing the message In Parameterized constructor With Integer Parameter: 2023

After the call to this(2023); it continues executing the next line, printing the message In Default constructor Of Employee Class.

 

Constructor chaining in Java using “super()” Keyword

If we want to chain constructors of the parent class, the super keyword can be used.

The below example demonstrates Constructor chaining in Java using the “Super()” Keyword. This type of constructor chaining in Java occurs using inheritance. Using the super keyword we can call the constructor of the parent class from a derived class.

 

Code Example:

//Constructor Chaining From Different Class

// Parent Class

 

class Employee {

// Default constructor

Employee() {

System. out.println(“In Default constructor Of Employee Class”);

}

 

// Parameterised constructor with String Parameter

Employee(String employeeName) {

System.out.println(“In Parameterised constructor Of Employee Class With String Parameter: ” + employeeName);

}

 

}

 

// Derived Class

 

public class SoftwareDeveloper extends Employee {

 

// Default constructor

SoftwareDeveloper() {

super(“Ankush”);

System. out.println(“In Default constructor Of SoftwareDeveloper Class”);

}

 

public static void main(String arg[]) {

SoftwareDeveloper softwareDeveloperObj = new SoftwareDeveloper();

}

}

 

Output:

  • In Parameterised constructor of employee class with String Parameter: Ankush.
  • In default constructor of SoftwareDeveloper class.

 

Code Explanation:

  • In the above program, when the SoftwareDeveloper class object is created without parameters, the default constructor of SoftwareDeveloper Class is called.
  • Within the default constructor, the parametrized constructor of the parent class i.e. Employee class is called using super(“Ankush”),
  • Thus, control goes into the parent class constructor before the print statement of the derived class constructor is invoked.
  • Thus, the print statement of the parameterized parent class constructor is invoked first, and then the derived class default constructor is called.

 

With this, we wrap up our 2024 guide on What is Constructor Chaining in Java. This is a noteworthy concept for any Java developer and this guide would help you be equipped in creating efficient and robust object structures. Constructor Chaining in Java is not just a technique, but a cornerstone of programming that helps build better software. So, continue your coding journey with this additional knowledge of constructor chaining and the endless possibilities it unlocks!

 

Read Next: The Data Modeling Beginner’s Guide for 2024

Comments are closed