How many objects are created due to inheritance in java?

前端 未结 14 1637
长情又很酷
长情又很酷 2020-11-29 18:48

Let\'s say I have three classes:

class A {
    A() {
        // super(); 
        System.out.println(\"class A\");
    }
}
class B extends A {
    B() {
             


        
14条回答
  •  眼角桃花
    2020-11-29 19:42

    If you look at dynamics of object allocation as per this SO answer, it must be clear that using new operator, you create only one object per statement. To further clarify the doubt that there is only one object which is being created, go thru this program:

    public class A {
        public static int aInstanceCount=0;
        public static A aInstance;
        public String aInstanceVariable;
        A() {
    //Super();
            aInstanceCount++;
            aInstanceVariable="aInstanceVar";
            System.out.println("class A");
            aInstance=this;
        }
    }
    
    class B extends A {
        public static int bInstanceCount=0;
        public static B bInstance;
        public String bInstanceVariable;
        B() {
    //Super();
            bInstanceCount++;
            bInstanceVariable="bInstanceVar";
            System.out.println("class B");
            bInstance=this;
        }
    }
    
    class C extends B {
        public static void main(String args[]) {
            int instanceCount=0;
            C c = new C(); //Parent constructor will get call
            if(A.aInstance!=null){
                instanceCount++;
                System.out.println("Value of aInstanceVariable: "+A.aInstance.aInstanceVariable);
    
            }
            if(B.bInstance!=null){
                instanceCount++;
                System.out.println("Value of bInstanceVariable: "+B.bInstance.bInstanceVariable);
            }
            A a=A.aInstance;
            B b=B.bInstance;
            System.out.println("bInstanceVariable of B earlier: " + B.bInstance.bInstanceVariable);
            //Now we are changing the bInstanceVariable of c which is inherited from B
            c.bInstanceVariable="bInstance After modified by C";
            System.out.println("bInstanceVariable of B after: " + B.bInstance.bInstanceVariable);
            System.out.println("aInstanceVariable of A earlier: " + A.aInstance.aInstanceVariable);
            //Now we are changing the aInstanceVariable of c which is inherited from A
            c.aInstanceVariable="aInstance After modified by C";
            System.out.println("bInstanceVariable of A after: " + A.aInstance.aInstanceVariable);
        }
    }
    

    The output:

    class A
    class B
    Value of aInstanceVariable: aInstanceVar
    Value of bInstanceVariable: bInstanceVar
    bInstanceVariable of B earlier: bInstanceVar
    bInstanceVariable of B after: bInstance After modified by C
    aInstanceVariable of A earlier: aInstanceVar
    bInstanceVariable of A after: aInstance After modified by C
    

    If you can notice, the super constructor is implicitly getting called each time if a subclass object is created, but since the new operator is used only once, there is only one object which is actually allocated the space. And by modifying the aInstanceVariable via C object c, we are actually changing the aInstanceVariable of aInstance. So it clearly proves that there is actually one object.

提交回复
热议问题