28
AugConstructor Chaining in Java
What is constructor chaining in Java?
We assume you already know what are Constructors in Java. They are the special methods which help in initializing newly created objects. One of the concepts related to constructors is Constructor Chaining.
- Constructor Chaining is a mechanism in Java where one constructor calls another constructor within the same class or in its superclass.
- It works like a chain, where one constructor leads to another and the execution of multiple constructors occurs in a sequence.
- To chain constructors within the same class, we use this() keyword and super() to call constructor from the parent class.
Why Do We Need Constructor Chaining in Java?
Constructor Chaining in Java is needed to streamline the process of object creation when a class has multiple constructors with different parameters. Instead of writing the same initialization logic in every constructor, Java allows one constructor to call another using this() for the current class or super() for the parent class. This reduces code duplication and makes your code cleaner and easier to maintain.
Using constructor chaining improves code reusability, enhances readability, and ensures that initialization happens in a consistent and logical order. It also helps in avoiding common programming errors by centralizing shared setup logic in a single place. This is especially useful in real-world applications where classes often need to be initialized in different ways based on various conditions.
Read More - Advanced Java Interview Questions And Answers
Read More - Java Multithreading Interview Questions
Types of Constructor Chaining
- Chaining within the same class this type of constructor chaining in Java, chaining occurs within the same class, that is, a constructor calls another constructor of the same class to which it belongs.
- Chaining with Superclass Constructors- With the help of Inheritance, a subclass can inherit from a superclass. Constructor Chaining with superclass constructors is when a constructor in a subclass calls a constructor from its superclass using the ‘super()’ keyword. This ensures proper execution of initialization tasks in both superclass and subclass when an object is created in the subclass.
Syntax
public class MyClass {
// Constructor with parameters
public MyClass(int param1, String param2) {
// Some initialization or processing
// Call another constructor within the same class
this(param1);
// Additional initialization or processing
}
// Constructor with a different set of parameters
public MyClass(int param1) {
// Some initialization or processing
// Additional initialization or processing
}
}
Syntax
public class Subclass extends Superclass {
// Constructor with parameters
public Subclass(int param1, String param2) {
// Call superclass constructor with parameters
super(param1, param2);
// Additional initialization or processing
}
}
How Constructor Chaining Works in Java?

Example on our Java Compiler
public class MyClass {
private int value1;
private String value2;
// Constructor with parameters
public MyClass(int value1, String value2) {
// Call another constructor within the same class
this(value1);
// Some initialization or processing
this.value2 = value2;
// Additional initialization or processing
System.out.println("Constructor with int and String parameters called.");
}
// Constructor with a different set of parameters
public MyClass(int value1) {
// Some initialization or processing
this.value1 = value1;
// Additional initialization or processing
System.out.println("Constructor with int parameter called.");
}
public void displayValues() {
System.out.println("Value1: " + value1);
System.out.println("Value2: " + value2);
}
public static void main(String[] args) {
// Creating an instance of MyClass
MyClass obj = new MyClass(10, "Hello");
// Displaying values
obj.displayValues();
}
}
In the above example,
- There are two constructors in the class MyClass, one has two parameters and another only one.
- The constructor that has two parameters, int and string, calls the other constructor that has only one parameter, int using the this() keyword.
Output
Constructor with int parameter called.
Constructor with int and String parameters called.
Value1: 10
Value2: Hello
Chaining Constructors in Superclasses
class Vehicle {
private String brand;
private String model;
// Constructor with parameters
public Vehicle(String brand, String model) {
this.brand = brand;
this.model = model;
System.out.println("Vehicle constructor called with brand: " + brand + ", model: " + model);
}
}
class Car extends Vehicle {
private int year;
// Constructor with parameters
public Car(String brand, String model, int year) {
super(brand, model); // Call superclass constructor with parameters
this.year = year;
System.out.println("Car constructor called with brand: " + brand + ", model: " + model + ", year: " + year);
}
}
public class Main {
public static void main(String[] args) {
// Creating an instance of Car
Car car = new Car("Toyota", "Corolla", 2020);
}
}
In the above example,
- There is a superclass Vehicle, which has attributes as brandand model.
- It has a subclass Car, with another attribute year.
- Using the super() keyword, the constructor of the Car class calls the constructor of Vehicle class.
- In the main()method, when an instance of Caris created, both the constructors of Car and Vehicle are called.
Output
Vehicle constructor called with brand: Toyota, model: Corolla
Car constructor called with brand: Toyota, model: Corolla, year: 2020
Implicit Super Constructor Call
Example:
class Superclass {
int num;
// Superclass constructor
public Superclass() {
System.out.println("Superclass constructor called.");
num = 10;
}
}
class Subclass extends Superclass {
// Subclass constructor
public Subclass() {
// Call to superclass constructor
super();
System.out.println("Subclass constructor called.");
}
// Method to display the value of num
public void display() {
System.out.println("Value of num in superclass: " + num);
}
}
public class Main {
public static void main(String[] args) {
// Creating an object of Subclass
Subclass obj = new Subclass();
// Calling display method
obj.display();
}
}
Explanation:
- We have two classes here, ‘Superclass’ and ‘Subclass’ in our Java Online Editor.
- Subclass inherits from Superclass.
- Superclass has a default constructor that initializes the variable ‘num’ to 10.
- Subclass does not define any constructors explicitly.
- When an object of Subclass is created (‘Subclass obj = new Subclass();’), the constructor of Superclass is implicitly called before the constructor of Subclass.
- The Java compiler calls the superclass constructor implicitly by itself and ‘Superclass’ execution occurs first, before the execution of the ‘Subclass’.
Output
Superclass constructor called.
Subclass constructor called.
Value of num in superclass: 10
Advantages of Constructor Chaining
- Code Reusability – Constructor chaining enhances code reusability by eliminating duplicate initialization logic. Instead of rewriting similar code in multiple constructors, one constructor can call another, streamlining the object creation process.
- Improved Code Organization – By chaining constructors, your Java code becomes cleaner and better organized. It enhances readability and helps developers quickly understand how different constructors relate to one another.
- Enhanced Flexibility – Constructor chaining supports multiple constructors with different parameter sets, offering flexibility in how objects are initialized. This allows developers to provide varied ways of creating objects based on specific use cases.
- Proper Initialization Order – When constructors are chained, it ensures that superclass constructors or common initialization logic execute before subclass-specific or specialized logic. This guarantees that objects are always initialized in the correct and consistent order.

- Constructor Call Must Be the First Statement- The call to another constructor—either using this() (same class) or super() (parent class)—must be the very first statement inside a constructor. Placing it elsewhere will result in a compilation error.
- Only One Constructor Call Allowed- A constructor can only invoke one other constructor. This means you can’t use both this() and super() in the same constructor. Only a single constructor call is permitted.
- No Circular Constructor Calls- Constructor chaining must not result in a circular chain, where a constructor ends up calling itself either directly or indirectly. This will cause an infinite loop and result in a compile-time error or runtime exception.
Using the init() method for Constructor Chaining
Example
class MyClass {
private int value1;
private String value2;
// Constructor with parameters
public MyClass(int value1, String value2) {
// Perform some initialization or processing
init(value1, value2);
System.out.println("Constructor with int and String parameters called.");
}
// Constructor with a different set of parameters
public MyClass(int value1) {
// Perform some initialization or processing
init(value1, "Default");
System.out.println("Constructor with int parameter called.");
}
// Private initialization method
private void init(int value1, String value2) {
this.value1 = value1;
this.value2 = value2;
// Additional initialization or processing can be done here
}
public void displayValues() {
System.out.println("Value1: " + value1);
System.out.println("Value2: " + value2);
}
public static void main(String[] args) {
// Creating an instance of MyClass
MyClass obj1 = new MyClass(10, "Hello");
MyClass obj2 = new MyClass(20);
// Displaying values
obj1.displayValues();
obj2.displayValues();
}
}
In the above example,
- We have used the private init() method for initializing the instance variables.
- There are two constructors in MyClass and both of them call the init method with parameters needed.
Output
Constructor with int and String parameters called.
Constructor with int parameter called.
Value1: 10
Value2: Hello
Value1: 20
Value2: Default
Common errors in Constructor chaining
Constructor chaining is helpful, but beginners often make some common mistakes. Here's a simple list of what to avoid:
- Constructor Calling Itself Again and Again- This happens when two or more constructors keep calling each other in a loop. It causes the program to crash with an error.
- Wrong Placement of this() or super()- In Java, the this() or super() call must always be the first line inside a constructor. Placing it after any other line causes an error.
- Using Both this() and super() Together- You can only use one constructor call at the start. If you try to use both this() and super() in the same constructor, Java will show an error.
- Calling a Constructor That Doesn’t Exist- If you try to call a constructor with parameters that you haven’t defined, Java won’t understand it and will give a compile-time error.
Summary
FAQs
- Use ‘this()’ keyword with appropriate arguments to call another constructor in the same class.
- Ensure that the call to another constructor is the first statement in the constructor where it is called.
Take our Java skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.