Constructor chaining
Q. Constructor chaining is- Published on 19 Oct 15a. subclass constructor calling super class constructor
b. super class constructor calling subclass constructor
c. both
d. none
ANSWER: subclass constructor calling super class constructor
In Inheritance subclass or derived class inherits properties of super class or parent class. Constructor chaining is the property by which a constructor in derived class(s) can call constructor of parent class(s). In constructor chaining, when an object of derived class is created its constructor is called which further calls the constructor method of parent class. The keyword super in java helps in passing arguments to super class constructor. The creation of subclass object starts with initialization of class(s) above it in inheritance chain. There could be any number of classes in the inheritance chain. Every constructor function will call up the chain till the top most class is reached.
For Example:
public class animal
{
private string str;
public animal( string str)
{
this.name= name;
system.out.println(“I am an Animal”);
}
}
public class lion extends animal
{
public lion( string str)
{
super(str);
system.out.println(“I am a lion”);
}
}