What is Constructor Chaining? Explained
Constructor chaining explained — this() vs super(), first-statement rule and implicit super() — with a Java example and interview Q&A.
Expected Interview Answer
Constructor chaining is the technique of one constructor invoking another constructor — either in the same class via `this(...)` or in the parent class via `super(...)` — so shared initialization logic runs once instead of being duplicated across every constructor.
When a class offers several overloaded constructors, each one can delegate to a more complete constructor that does the real field assignment, with the caller only supplying defaults for the missing arguments. In Java, a chaining call must be the very first statement in the constructor body, and a constructor may chain to exactly one other constructor (never both `this(...)` and `super(...)` in the same body). If no explicit chaining call is written, the compiler silently inserts a no-argument `super()` call to the parent, which is why a parent class without a no-arg constructor forces every subclass constructor to chain explicitly. The overall effect is a single, controlled initialization path: subclass work builds on top of parent work, which builds on top of the most specific same-class constructor.
- Eliminates duplicated field-assignment logic across overloaded constructors
- Guarantees parent state is initialized before subclass state
- Centralizes validation in one canonical constructor
- Makes the initialization order explicit and predictable
AI Mentor Explanation
A franchise’s net-practice booking form lets a player request just a time slot, and that short form internally fills in the default net, default coach and default equipment before handing off to the full booking form that actually reserves everything. The player never has to fill every field themselves for a quick booking. This is constructor chaining: the short-form constructor delegates to the fuller constructor with sensible defaults, so the detailed setup logic exists in exactly one place.
Step-by-Step Explanation
Step 1
Define the most complete constructor
Write the constructor that accepts every parameter and performs the real field assignment and validation.
Step 2
Add overloaded constructors
Create simpler constructors for common cases with fewer parameters.
Step 3
Chain with this(...) or super(...)
The first statement of a simpler constructor calls the complete constructor (same class) or the parent constructor.
Step 4
Let the chain resolve top-down
super(...) chains run first, establishing parent state before the current constructor body executes.
What Interviewer Expects
- Correct distinction between this(...) chaining and super(...) chaining
- Knowledge that the chaining call must be the first statement
- Awareness that a constructor cannot call both this(...) and super(...)
- Understanding of the implicit super() insertion when no chaining call is written
Common Mistakes
- Believing this(...) and super(...) can both appear in one constructor
- Placing the chaining call anywhere other than the first statement
- Not knowing the compiler inserts an implicit super() when omitted
- Duplicating validation logic across constructors instead of chaining
Best Answer (HR Friendly)
“Constructor chaining is when one constructor calls another constructor to avoid repeating setup code. It can call another constructor in the same class with this(...), or the parent class constructor with super(...), and that call always has to be the very first line. It keeps initialization logic in one place instead of copy-pasting it across every overloaded constructor.”
Code Example
class Vehicle {
protected String brand;
Vehicle(String brand) {
this.brand = brand;
}
}
class Car extends Vehicle {
int seats;
Car(String brand) {
this(brand, 4); // chains to the two-arg constructor below
}
Car(String brand, int seats) {
super(brand); // chains to Vehicle’s constructor
this.seats = seats;
}
}
Car c1 = new Car("Toyota"); // seats defaults to 4
Car c2 = new Car("Toyota", 2); // explicit seat countFollow-up Questions
- Can a constructor call both this(...) and super(...)?
- What happens if a parent class has no no-argument constructor?
- Why must the chaining call be the first statement in a constructor?
- How does constructor chaining differ from method overloading?
MCQ Practice
1. Where must a this(...) or super(...) chaining call appear in a constructor?
A chaining call must be the first statement in the constructor body; the compiler enforces this.
2. Can a single constructor call both this(...) and super(...)?
A constructor can chain to only one other constructor — either this(...) or super(...), never both.
3. If a constructor has no explicit this(...) or super(...) call, what happens?
The compiler automatically inserts a call to the parent’s no-argument constructor if none is written explicitly.
Flash Cards
Constructor chaining in one line? — One constructor calling another (this(...) or super(...)) to reuse initialization logic.
Where must the chaining call sit? — As the first statement in the constructor body.
Can this() and super() both be used in one constructor? — No — only one chaining call is allowed per constructor.
What happens with no explicit chaining call? — The compiler implicitly inserts super() to the parent’s no-arg constructor.