Alex Rivera | Logout

How many objects are created due to inheritance in java?

Asked 2013-07-26T09:23:36.477
61

Let's say I have three classes:

class A {
    A() {
        // super(); 
        System.out.println("class A");
    }
}
class B extends A {
    B() {
        // super(); 
        System.out.println("class B");
    }
}
class C extends B {
    public static void main(String args[]) {
        C c = new C(); //Parent constructor will get called
    }
}

When I create an instance of class C, it calls the constructor of super class. So, is there more than one object that is getting created? If only one object is created, then how is super() like another class' constructor? Does super() method internally create an object? What I know is, the constructor is also a method (I may be wrong).

My questions are:

  1. How many number of Object is created in this case?
  2. If one object is created then how does Super() internally call the parent class constructor?
Edit
Report

1 Answer

7
  1. There will be one and only one object will be created and ie. A object.

  2. You can imagine like when class A extends B, then all methods and variables are copied to class A.

answered 2013-07-26T09:37:22.757

Your Answer