๐ Introduction
In the previous articles, we explored:
- Inheritance
- Method Overriding
superkeyword
Now comes a very important concept that explains what happens during object creation:
๐ Constructors in Inheritance (Constructor Chaining)
Whenever we create an object of a child class:
Dog d = new Dog();
Many students wonder:
- Which constructor runs first?
- Parent or child?
- Do both run?
- How does Java decide?
๐ These questions are answered by constructor chaining.
Understanding this concept is extremely important because it explains how Java initializes objects step by step.
๐ง What is Constructor Chaining?
Constructor chaining means:
When a child class object is created, constructors of all parent classes are executed in a chain.
๐ก Key Idea
๐ Parent constructor runs before child constructor.
๐งฑ Basic Example (Default Constructors)
๐ป Code
class Animal {
Animal() {
System.out.println("Animal Constructor");
}
}
class Dog extends Animal {
Dog() {
System.out.println("Dog Constructor");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
}
}
๐ Step-by-Step Execution
Dog d = new Dog();
Execution flow:
1๏ธโฃ Dog constructor is called
2๏ธโฃ Before executing Dog constructor โ Java calls Animal constructor
3๏ธโฃ Then Dog constructor runs
๐งพ Output
Animal Constructor
Dog Constructor
๐ง Important Concept
Even if you do not write super(), Java automatically inserts:
super();
as the first line of child constructor.
๐น Using super() Explicitly
๐ป Code
class Animal {
Animal() {
System.out.println("Animal Constructor");
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("Dog Constructor");
}
}
๐ Explanation
super();
๐ Calls parent constructor explicitly
๐ Must be the first statement
๐ฅ Parameterized Constructor with super()
๐ป Code
class Animal {
Animal(String name) {
System.out.println("Animal: " + name);
}
}
class Dog extends Animal {
Dog() {
super("Dog Parent");
System.out.println("Dog Constructor");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
}
}
๐ Explanation
super("Dog Parent");
๐ Calls parameterized constructor of parent
๐งพ Output
Animal: Dog Parent
Dog Constructor
โ ๏ธ Important Rule
If parent has only parameterized constructor, then:
๐ Child must call it using super(parameters)
Otherwise โ โ Compile-time error
๐ซ Common Error (Very Important)
๐ป Code
class Animal {
Animal(String name) {
System.out.println(name);
}
}
class Dog extends Animal {
Dog() {
System.out.println("Dog");
}
}
โ Error
Constructor Animal() is undefined
๐ง Why?
๐ Java tries to call:
super();
But parent has no default constructor
โ Fix
Dog() {
super("Animal");
System.out.println("Dog");
}
๐ฟ Multilevel Constructor Chaining
๐ป Code
class Animal {
Animal() {
System.out.println("Animal Constructor");
}
}
class Dog extends Animal {
Dog() {
System.out.println("Dog Constructor");
}
}
class Puppy extends Dog {
Puppy() {
System.out.println("Puppy Constructor");
}
}
public class Main {
public static void main(String[] args) {
Puppy p = new Puppy();
}
}
๐ Execution Flow
Animal โ Dog โ Puppy
๐งพ Output
Animal Constructor
Dog Constructor
Puppy Constructor
๐ง Important Insight
๐ Constructors execute top to bottom (parent โ child)
โ๏ธ Rules of Constructor Chaining
โ Rule 1: Parent Constructor Runs First
Always:
Parent โ Child
โ Rule 2: super() Must Be First Statement
Dog() {
super();
}
โ Rule 3: If Not Written, Java Inserts super()
Default behavior.
โ Rule 4: Only One Constructor Call Allowed
You cannot write both:
this();
super(); // โ not allowed together
โ Rule 5: Constructor is Not Inherited
Child class does NOT inherit constructors.
๐ this() vs super()
| Feature | this() | super() |
|---|---|---|
| Refers to | Same class constructor | Parent class constructor |
| Use | Constructor chaining within class | Parent constructor call |
| Position | First line | First line |
๐งจ Tricky Interview Example
class A {
A() {
System.out.println("A");
}
}
class B extends A {
B() {
System.out.println("B");
}
}
public class Main {
public static void main(String[] args) {
B obj = new B();
}
}
๐งพ Output
A
B
๐ Parent runs first โ always
โ ๏ธ Common Mistakes & Confusions
โ Mistake 1: Thinking child constructor runs first
โ Wrong
โ Parent always runs first
โ Mistake 2: Forgetting parameterized constructor
Leads to compile error
โ Mistake 3: Writing super() after statements
Dog() {
System.out.println("Dog");
super(); // โ Error
}
โ Mistake 4: Thinking constructors are inherited
โ Constructors are NOT inherited
โ Mistake 5: Using both this() and super()
โ Not allowed together
๐ฏ Real-Life Analogy
Think of building a house:
Foundation โ Walls โ Roof
๐ Foundation (parent) must be built first
๐ Then upper layers (child)
๐ง Key Points to Remember
โ Constructor chaining = Parent โ Child
โ super() is used to call parent constructor
โ Java automatically inserts super()
โ Parent constructor must exist
โ Executes in top-down order
๐ Conclusion
Constructors in inheritance explain how Java creates and initializes objects step by step.
This concept is extremely important because it:
- Defines object creation flow
- Ensures proper initialization
- Connects inheritance with real execution
- Helps avoid runtime and compile-time errors
In this article, we learned:
- What constructor chaining is
- Execution order of constructors
- Role of
super() - Parameterized constructor handling
- Common mistakes and interview traps
Mastering constructor chaining gives you a deep understanding of how Java works internally, making you a stronger programmer.