Constructor Chaining
About
Types of Constructor Chaining
1. Chaining Within the Same Class (this())
this())class Book {
String title;
int pages;
Book() {
this("Unknown", 0); // Calls the two-arg constructor
}
Book(String title, int pages) {
this.title = title;
this.pages = pages;
}
}2. Chaining Between Superclass and Subclass (super())
super())Why Constructor Chaining Is Important
1. Eliminates Redundancy
2. Centralizes Initialization Logic
3. Improves Readability and Maintainability
4. Ensures Proper Superclass Initialization
5. Supports Flexible Object Creation
Rules of Constructor Chaining
1. Constructor Call Must Be the First Statement
2. Only One Constructor Call Allowed
3. Constructor Chaining Cannot Be Recursive
4. Superclass Constructor Is Called Implicitly If Not Specified
5. Constructors Are Not Inherited
6. The Constructor Chain Ends at a Constructor That Does Not Chain Further
Examples
Example 1: Basic Constructor Chaining in Inheritance
Example 2: Parameterized Constructor in Superclass
Example 3: Multiple Levels of Inheritance
Example 4: Constructor Chaining Across Levels with Parameters
Example 5: Calling Overloaded Constructors with this() and super()
this() and super()Example 6: Real-World Modeled Example (Banking)
Common Errors and Misconceptions
Using this() and super() Together
this() and super() TogetherRecursive Constructor Calls
Last updated