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 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 constructorExample :-
Both
constructors in Animal.java
public
Animal(String animal)
{
System.out.println(animal);
}
public
Animal()
{
System.out.println(“No-arg
constructor”);
}
- Only one constructor can be called from child class,the main reason for this is super key word statement should always be the first statement,so two super statements cannot be there
- More on super key word @ http://java-recent.blogspot.in/2013/06/super-and-this-key-words-in-java_12.html
Happy
Learning
Please
provide your valuable comments on this article and share it.
Contact
me @ sudheer@javarecent.com
or admin@java-recent.com
No comments:
Post a Comment