Wednesday, June 12, 2013

Constructor flow -Inheritance

Constructor invocation flow in Java-Inheritance:-

Hi All,
Welcome to Java-recent.
In this post I will be explaining about how constructor invocation will happen in inheritance.
knowing the constructor flow is very important for interviews and written tests, I would be explaining this with examples.

We will be using Animal.java, Lion.java, Elephant.java .


Animal.java

public class Animal {
public Animal()
{
System.out.println("Hey this is super class constructor ");
}
}

Lion.java

public class Lion extends Animal{
public Lion()
{
System.out.println("In child class Lion constructor");
}
}

Elephant.java

public class Elephant extends Animal{
public Elephant()
{
System.out.println("in child class Elephant constructor");
}
}

Lion and Elephant class extends Animal class

Tester.java
public class Tester
{
public static void main(String[] args)
{
//Creating lion object
Lion lion=new Lion();
System.out.println("----------------------------");
//Creating animal object
Elephant animal=new Elephant();
}


Output when we run Tester.java

Hey this is super class constructor
In child class Lion constructor
----------------------------
Hey this is super class constructor
in child class Elephant constructor

Observe the output,when we created child class object implicitly super class constructor is called by JVM

Now if we replace with an augmented constructor then how will the flow occur.
Lets change the constructor code in Animal.java as below

 //Parameterised constructor in Parent class
public Animal(String animal)
{
System.out.println(animal);
}


Now there will error in Elephant.java and Lion.java
Because by default no-arg constructor of super class will be called.
If there is parameterised constructor we need to explicitly call them from child class using Key word super .

Lets see the changed code in both Lion.java and Elephant.java

Lion.java
public class Lion extends Animal{
public Lion()
{
super("From Lion");
System.out.println("In child class Lion constructor");
}
}

Elephant.java

public class Elephant extends Animal{
public Elephant()
{
super("From elephant");
System.out.println("in child class Elephant constructor");
}
}

We will run the program using Tester.java

Output of the program is as follows

From Lion
In child class Lion constructor
----------------------------
From elephant
in child class Elephant constructor


Key take away points :-

  • When an object of child class is created super class default constructor is invoked
  • If there is a parameterised constructor then we need to explicitly class from child class constructor using super key word
  • If you still need to invoke no-arg constructor we need to explicitly type one in parent constructor and invoke it using super() from child class constructor
    Example :-
Both constructors in Animal.java
public Animal(String animal)
{
System.out.println(animal);
}
public Animal()
{
System.out.println(“No-arg constructor”);
}


Happy Learning

Please provide your valuable comments on this article and share it.


No comments:

Post a Comment

Like and Share