Constructor Chaining in Java with Examples
## Introduction
Constructor chaining is a process of calling one constructor from another constructor with respect to current object. It is also known as constructor delegation. It is mainly used for code reusability or for code optimization. This process is also known as method chaining. In this article, we will discuss constructor chaining in Java with examples.
Constructor chaining is a useful concept in Java, as it allows a class to have more than one constructor that calls the same constructor with different parameters. This process helps in code reusability and optimizes the code. In constructor chaining, the constructors of the same class or its parent class are called. Constructor chaining is generally used to initialize the instance variables of a class. With the help of constructor chaining, object creation becomes faster, as the same constructor is called multiple times. It also helps to reduce the code size and complexity.
Constructor chaining is an important concept of object-oriented programming. It helps in code reusability and optimization as the same constructor is called multiple times with different parameters. This helps to reduce code size and complexity. Constructor chaining is used to initialize the instance variables of a class. It is also used to reduce the number of constructors that need to be written. Constructor chaining can be used to call the constructor of the same class or the constructor of the parent class. The constructors in the chain are executed in the same order as they are called.
Constructor chaining is an important concept in Java, as it helps in code optimization and reusability. It helps to reduce the number of constructors that need to be written and also helps to initialize the instance variables of a class. Constructor chaining can be used to call the constructor of the same class or the constructor of the parent class. The constructors in the chain are executed in the same order as they are called. In this article, we will discuss constructor chaining in Java with examples.
Worried About Failing Tech Interviews?
Attend our webinar on
"How to nail your next tech interview" and learn
.png)
Hosted By
Ryan Valles
Founder, Interview Kickstart

Our tried & tested strategy for cracking interviews

How FAANG hiring process works

The 4 areas you must prepare for

How you can accelerate your learnings
Register for Webinar
Constructor Chaining is the process of calling one constructor from another constructor of the same class or of its parent class. It is used to reuse the code and avoid the redundant code.
Syntax:
class class_name
{
// constructor 1
class_name()
{
// code to be executed
}
// constructor 2
class_name(parameters)
{
// code to be executed
this();
}
}
Example:
class Rectangle
{
int length;
int breadth;
// Default constructor
Rectangle()
{
System.out.println("Constructor Chaining Example");
}
// Parameterized constructor
Rectangle(int length, int breadth)
{
this.length = length;
this.breadth = breadth;
this();
}
}
public class Test
{
public static void main(String[] args)
{
// Creating an object using default constructor
Rectangle r1 = new Rectangle();
// Creating an object using parameterized constructor
Rectangle r2 = new Rectangle(10, 20);
}
}
Output:
Constructor Chaining Example