What is order of execution of constructors in Java inheritance?

Answer includes order of execution of constructors in Java inheritance hierarchy when we create child class object with java program single inheritance example and multilevel inheritance.

Answer: Order of execution of constructors in inheritance relationship is from base /parent class to derived / child class.

We know that when we create an object of a class then the constructors get called automatically.

In inheritance relationship, when we create an object of a child class, then first base class constructor and then derived class constructor get called implicitly.

NOTE: We cannot call a class constructor explicitly using an object or say manually.

Let’s see the constructors call example in two cases i.e. in single inheritance and multilevel inheritance program in Java.

Cases-1: Constructor call order in single inheritance java

If we create object of child class in main() program, constructors of parent class then child class will be called.

 

OUTPUT:
Constructor call order…
Parent()…
Child()…

Cases-1: Constructor call order in Multilevel inheritance

If we create object of bottom most derived class i.e. of Testing  class in main() program, then constructors of Design class, Coding class and then Testing class will be called.

 

OUTPUT:
Constructor call order…
Design()…
coding()…
Testing()…

Read More