DEV Community

Deepikandas
Deepikandas

Posted on

Constructors in Inheritance

How Constructors Work in Inheritance

When a subclass object is created, constructors of both parent and child are involved:

  • Parent class constructor executes first.
  • super() is implicitly added in every constructor of sub class.
  • Child class constructor executes next.
  • This ensures that parent class fields are initialized before child class fields.

  • Java compiler does not implicitly call parameterized parent class constructor unless we insert super(args) in subclass constructors.

  • If parent has only parameterized constructors, child must explicitly call one using super(args).

  • If you explicitly write super(args), then the compiler will NOT add super().
    Case 1

Case 2:

Case 3:


Case 4:


Case 5


Case 6:


Case 7:Explicitly super(args) written


Case 8 :If parent has only parameterized constructors, child must explicitly call one using super(args).

Top comments (0)