6-๐Ÿ”— Constructors in Inheritance in Java โ€“ Constructor Chaining Explained with Examples & Common Mistakes

๐ŸŒŸ Introduction

In the previous articles, we explored:

  • Inheritance
  • Method Overriding
  • super keyword

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()

Featurethis()super()
Refers toSame class constructorParent class constructor
UseConstructor chaining within classParent constructor call
PositionFirst lineFirst 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.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *