Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Inheritance. No Comments on Constructors in Inheritance

In this article we will look at the sequence in which constructors in inheritance are executed i.e., is the base class constructor invoked first or the derived class constructor is invoked first.

 

Consider two classes participating in simple inheritance. Let A be the super class and B be the sub class. Now, which constructor is invoked first? Is it the constructor of B followed by the constructor of A or vice versa?

 

The sequence in which the constructors are invoked is from super class to sub class i.e, first the constructor of class A is executed then constructor of class B is executed.

 

The sequence of constructor invocation does not change even when super keyword is used. Now let’s consider a few examples to understand how constructors in inheritance works.

 

Example 1:

class A
{
	A()
	{
		System.out.println("Class A's constructor is invoked");
	}
}

class B extends A
{
	B()
	{
		System.out.println("Class B's constructor is invoked");
	}
}

public class Driver
{
	public static void main(String[] args) 
	{
		A obj = new A();
	}
}

 

In the above example, object for class A is created. Since A is the super class only it’s constructor is invoked and the output will be:

 

Class A’s constructor is invoked

 

Now let’s look at another example.

 

Example 2:

class A
{
	A()
	{
		System.out.println("Class A's constructor is invoked");
	}
}

class B extends A
{
	B()
	{
		System.out.println("Class B's constructor is invoked");
	}
}

public class Driver
{
	public static void main(String[] args) 
	{
		B obj = new B();
	}
}

 

In the above example, we are creating object for the sub class B. Since B is a sub class, constructor of class A is invoked first and then constructor of class B is invoked. Output for above program is:

 

Class A’s constructor is invoked
Class B’s constructor is invoked

 

Now, let’s look at another example.

 

Example 3:

class A
{
	A()
	{
		System.out.println("Class A's constructor is invoked");
	}
}

class B extends A
{
	B()
	{
		System.out.println("Class B's constructor is invoked");
	}
}

class C extends B
{
	C()
	{
		System.out.println("Class C's constructor is invoked");
	}
}

public class Driver
{
	public static void main(String[] args) 
	{
		C obj = new C();
	}
}

 

In the above we can see multi-level inheritance A <- B <- C. The object we are creating is for sub class C. So the sequence in which constructors are executed is constructor of class A followed by class B followed by class C. Output of the above program is:

 

Class A’s constructor is invoked
Class B’s constructor is invoked
Class C’s constructor is invoked

 

I think above examples are enough to explain constructors in inheritance. If you have any doubts comment below.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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