
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Constructor Chaining In Java programming
The constructor chaining is a specific sequence of calling constructors when a user initializes an object in a certain way. This technique is used when multiple constructors are invoked one after another, based on the instance class. It is also closely related to inheritance, where the role of a subclass constructor is to call the constructor of its superclass.
You can perform constructor chaining in two ways:
- Within the same class - where one constructor calls another constructor of the same class.
-
Across different classes - where a subclass constructor calls a superclass constructor using the
super()
keyword.
Constructor Chaining in same Class
In this scenario, a constructor can call another constructor within the same class by using the this() keyword. This approach is particularly useful when you want to eliminate repetitive code and maintain a single, common initialization process that can be reused across multiple constructors. By doing so, you ensure consistency in object creation and make the code easier to maintain and extend.
Example
Following is an example of constructor chaining within the same class:
public class Student { private String name; private int age; // Constructor with name and age public Student(String name, int age) { this.name = name; this.age = age; } // Constructor with only name, default age public Student(String name) { this(name, 18); // Calls the first constructor with default age } public void displayData() { System.out.println("Name: " + this.name); System.out.println("Age: " + this.age); } public static void main(String[] args) { Student std1 = new Student("Zoro", 20); std1.displayData(); Student std2 = new Student("Luffy"); std2.displayData(); } }
Following is the output of the above code:
Name: Zoro Age: 20 Name: Luffy Age: 18
Constructor Chaining Across Different Classes
When a subclass is created, its constructor can call the constructor of its superclass using the super() keyword. This is particularly helpful because it ensures that all the properties and behaviors defined in the parent class are properly set up before the subclass adds its own specific features. In simple terms, super() allows the subclass to reuse the initialization logic of the parent class instead of rewriting it.
Example
Following is an example of constructor chaining across different classes:
public class Animal { protected String type; // Constructor of superclass public Animal(String type) { this.type = type; } public void displayType() { System.out.println("Animal Type: " + this.type); } } public class Dog extends Animal { private String name; // Constructor of subclass public Dog(String name) { super("Dog"); // Calls the constructor of Animal this.name = name; } public void displayInfo() { System.out.println("Dog Name: " + this.name); displayType(); } public static void main(String[] args) { Dog dog = new Dog("Buddy"); dog.displayInfo(); } }
Following is the output of the above code:
Dog Name: Buddy Animal Type: Dog
In conclusion, constructor chaining means calling one constructor from another also called constructor delegation. It is a powerful feature in Java that allows you to create more maintainable and reusable code by reducing redundancy in object initialization. We looked at two main types of constructor chaining: within the same class using this()
and across different classes using super()
.