What is order of execution of constructors in Java inheritance?

(Last Updated On: April 24, 2019)

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. Recommended to read class constructor concept in java.

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 an object of the child class in the program, then, the body of constructor of parent class will execute first, then body of child class will be executed. In simple word, we can say that the parent constructor get called first, then of  the child class.

Have a look at this constructor call orders image give below.

In the picture, there are parent and child class with main program. And with “new child() ” statement the constructor has been invoked.  How the controls flows in the program for parent child constructors calls is shown by arrow diagram.

Here are the steps how the program control flows in the image.

  1. You create an object of child class in main() as “new Child()”
  2. Control goes to child constructor, but, its body is not getting executed.
  3. Control goes to parent constructor, body of it get executed.
  4. Then, control comes back to child constructor and body get executed.
  5. Then, the controls come back to “new Child()” statement and exit.
order of execution of constructors in Java inheritance

order of execution of constructors in Java inheritance

 

Java code example

class Parent {
	
	Parent(){
		
		System.out.println("Parent()...");
	}

}

class Child extends Parent {
	
	Child(){
		
		System.out.println("Child()...");
	}

}

public class TestConstructorCallOrder {

	public static void main(String[] args) {
		
		//Create object of Child class object
		System.out.println("Constructor call order...");
		new Child();	

	}

}

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

 

Cases-2: 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.

Java code example

class Design {
	
	Design(){
		
		System.out.println("Design()...");
	}

}

class Coding extends Design {
	
	 Coding(){
		
		System.out.println("coding()...");
	}

}

class Testing extends Coding {
	
	Testing()
	{
		
	System.out.println("Testing()...");
	}

}


public class TestConstructorCallOrder {

	public static void main(String[] args) {
		
		//Create object of bottom most class object
		System.out.println("Constructor call order...");
		new Testing();	

	}

}

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