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 the inheritance relationship, when we create an object of a child class, then the first base class constructor and then the derived class constructor gets called implicitly.

NOTE: We cannot call a class constructor explicitly using an object or say manually. I recommend you to read the 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 the constructor of the parent class will execute first, then the body of the child class will be executed. In simple words, we can say that the parent constructor gets called first, then of the child class.

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

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

Here are the steps of 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 the child constructor, but, its body is not getting executed.
  3. Control goes to the parent constructor, the body of it gets executed.
  4. Then, control comes back to the child constructor and the body gets executed.
  5. Then, the controls come back to the “new Child()” statement and exit.
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 an object of the bottom-most derived class i.e. of the Testing class in the 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()…

Related Posts