Program:-
class Base1{
Base1(){
System.out.println("I am a constructor");
}
Base1(int a){
System.out.println("I am a overloaded Constructor from Base1 of a as : "+ a);
}
}
class Derived1 extends Base1{
Derived1(){
System.out.println("I am a derived constructor ");
}
Derived1(int a, int b){
super(a); //if we not use the super keyword here then overloaded constructor of Base class is not printed instead it prints the normal constructor from the base class.
System.out.println("I am a overloaded Constructor from derived1 of b as : "+b);
}
}
class ChildOfDerived extends Derived1{
ChildOfDerived(){
System.out.println("I am a constructor from ChildOfDerived class");
}
ChildOfDerived(int a, int b, int c){
super(a,b);
System.out.println("I am a overloaded Constructor from ChildOfDerived of c as :" + c);
}
}
public class Inheritance_Constructor46 {
public static void main(String[] args) {
Base1 a = new Base1(); //this print only the constructor of the base1 class.
System.out.println();
Derived1 b = new Derived1(); // this print both the constructor in Base1 and Derived1 class.
System.out.println();
Base1 c = new Base1(12); // this print the overloaded constructor from bas1 class.
System.out.println();
Derived1 d = new Derived1(12,13); // this print the overloaded constructor of both classes.
System.out.println();
ChildOfDerived e = new ChildOfDerived(); // this print the constructor from all three classes.
System.out.println();
ChildOfDerived f =new ChildOfDerived(12,13,14);// this print the overloaded constructor from all three classes.
}
}
