
In Java, we know that constructors play a crucial role in object creation. They initialize the state of objects when you create an instance of a class. One important feature associated with Java constructors is constructor chaining.
Constructor chaining refers to the process of calling one constructor from another constructor within the same class or between subclasses. It is a powerful mechanism that can enhance code reusability and improve clarity in your Java application programs.
In this article, we will explore constructor chaining from basic to advanced concepts, including examples, best practices, and frequently asked questions (FAQs) about the topic.
What is Constructor Chaining in Java?
Constructor chaining in Java is the process of invoking one constructor from another constructor in the same class or in its parent class. This helps in reusing code and reducing redundancy.
Key Points:
(1) You can achieve constructor chaining using this() keyword for calling a constructor within the same class or super() keyword to call a constructor of the parent class.
(2) You must write this() or super() keyword in the statement in a constructor to create constructor chaining.
(3) If no constructor is explicitly called, Java inserts a default super() call (invoking the no-arg parent constructor).
Types of Constructor Chaining in Java
A. Within the same class:
You use this() keyword when you want to call one constructor from another constructor in the same class. Let’s understand it with the help of an example program.
Example 1:
public class Student { String name; int age; String university; // Default constructor public Student() { this("John Doe"); // Calls the next constructor } // Constructor with name public Student(String name) { this(name, 20); // Calls the next constructor } // Constructor with name and age public Student(String name, int age) { this(name, age, "MIT"); // Calls the next constructor } // Full constructor public Student(String name, int age, String university) { this.name = name; this.age = age; this.university = university; } public static void main(String[] args) { Student s1 = new Student(); // Uses constructor chaining System.out.println(s1.name + ", " + s1.age + ", " + s1.university); } }
Output: John Doe, 20, MIT
B. Across the class hierarchy:
When a child constructor calls a parent class constructor in the parent class, then use super() keyword. Let’s take an example to understand this.
Example 2:
class Person { String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } } class Employee extends Person { double salary; public Employee(String name, int age, double salary) { super(name, age); // Calls parent constructor this.salary = salary; } public static void main(String[] args) { Employee emp = new Employee("Alice", 30, 50000.0); System.out.println(emp.name + ", " + emp.age + ", " + emp.salary); } }
Output: Alice, 30, 50000.0
Why Use Constructor Chaining?
There are several advantages of using constructor chaining in Java that are as follows:
a) Reusability: This allows you to avoid code duplication by calling another constructor to initialize the object with default or specific values.
b) Readability: Constructor chaining can improve the clarity of your code by reducing redundancy.
c) Maintainability: Using constructor chaining makes it easier to maintain your code because the initialization logic is centralized.
Constructor Chaining: Basic Example
Let’s begin with an example of constructor chaining within the same class. In this example, we’ll create a class Student with multiple constructors.
Example 3: Constructor Chaining in the Same Class
class Student { String name; int age; // Constructor 1 public Student() { this("Unknown", 0); // Calling Constructor 2 } // Constructor 2 public Student(String name) { this(name, 18); // Calling Constructor 3 } // Constructor 3 public Student(String name, int age) { this.name = name; this.age = age; System.out.println("Student created: " + name + ", Age: " + age); } public static void main(String[] args) { Student student1 = new Student(); Student student2 = new Student("John"); Student student3 = new Student("Alice", 20); } }
Output: Student created: Unknown, Age: 0 Student created: John, Age: 18 Student created: Alice, Age: 20
In this example, constructor chaining allows us to minimize redundant code, such as repeating the initialization logic of the student’s name and age across different constructors. First, the default constructor calls Constructor 2 with default values for the name and age.
The constructor that takes only the name parameter calls Constructor 3 to set the name and the default age. The most detailed constructor takes both the name and age and prints the student details.
Constructor Chaining: Calling Parent Class Constructors
Now let’s explore constructor chaining in a class hierarchy, where a subclass constructor calls a constructor in its parent class using super().
Example 4: Constructor Chaining Across Classes (Parent-Child)
class Animal { String name; // Constructor in the parent class public Animal(String name) { this.name = name; System.out.println("Animal Name: " + name); } } class Dog extends Animal { int age; // Constructor in the child class public Dog(String name, int age) { super(name); // Calling the parent class constructor this.age = age; System.out.println("Dog Age: " + age); } public static void main(String[] args) { Dog dog = new Dog("Buddy", 3); } }
Output: Animal Name: Buddy Dog Age: 3
In this example, the Animal class has a constructor that initializes the name of the animal. The Dog class, which extends the Animal class, has a constructor that initializes both the name and age. It calls the parent class constructor using super(name) to initialize the name attribute.
Here, constructor chaining ensures that the initialization logic in the parent class is executed before the subclass’s constructor logic is executed.
If you want to learn Java Programming from basic to advanced, then visit Scientech Easy Java.
Constructor Chaining with Default Constructors
In Java, every class has a default constructor, which is a no-argument constructor provided by the compiler if no other constructors are explicitly defined. If you define any constructor (like a parametrized one), the default constructor is no longer available unless explicitly defined.
Example 5: Constructor Chaining with Default Constructors
class Car { String model; int year; // Default constructor public Car() { this("Unknown", 0); // Constructor chaining } // Parameterized constructor public Car(String model, int year) { this.model = model; this.year = year; System.out.println("Car Model: " + model + ", Year: " + year); } public static void main(String[] args) { Car car1 = new Car(); Car car2 = new Car("Tesla", 2025); } }
Output: Car Model: Unknown, Year: 0 Car Model: Tesla, Year: 2025
FAQs on Constructor Chaining
1. Can constructor chaining be used across different classes?
Yes, constructor chaining can be used across different classes through inheritance. The child class can call the parent class constructor using the super() keyword.
2. What happens if we don’t use super() keyword in a subclass constructor?
If you don’t explicitly call super(), Java automatically calls the no-argument constructor of the parent class. If the parent class doesn’t have a no-argument constructor, a compilation error will occur.
3. Is constructor chaining required in Java?
No, constructor chaining is not mandatory. However, it is a useful technique for avoiding code repetition and improving code maintainability.
4. Can constructor chaining be used without inheritance?
Yes, constructor chaining can be used within a single class using this(). This allows you to call one constructor from another within the same class.
5. What happens if constructor chaining exceeds the call limit?
Java imposes a limit on the number of constructor calls that can occur during constructor chaining. If this limit is exceeded, a StackOverflowError will occur.