Calling of Constructors in a Java

In the book Java: The complete reference

// Demonstrate when constructors are called.
// Create a super class.
class A {
     A() {
        System.out.println("Inside A's constructor.");
     }
}
// Create a subclass by extending class A.
class B extends A {
      B() {
             System.out.println("Inside B's constructor.");
      }
 }
// Create another subclass by extending B.
class C extends B {
    C() {
        System.out.println("Inside C's constructor.");
    }
}
class CallingCons {
    public static void main(String args[]) {
        C c = new C();
    }
}

Output:
Inside A’s constructor
Inside B’s constructor
Inside C’s constructor

It is demonstrating how the constructor of a subclass is called. But why are constructors of the super class called in the absence of a super() constructor.

Why did the Java Language designers consider it necessary to do so?


Source: oop

Leave a Reply