Constructor chaining

Q.  Constructor chaining is
- Published on 19 Oct 15

a. 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)
{ name;
system.out.println(“I am an Animal”);
public class lion extends animal
public lion( string str)
system.out.println(“I am a lion”);

Post your comment / Share knowledge

Enter the code shown above:
(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)