How many objects are created due to inheritance in java?

前端 未结 14 1625
长情又很酷
长情又很酷 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.

    0 讨论(0)
  • 2020-11-29 19:43
    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.

    0 讨论(0)
  • 2020-11-29 19:43

    If you add one more line of Code System.out.println(this.hashCode()) will remove your confusion.

    Here in All Case hashCode() will print same hashCode all the time. It means there is one and only one unique Object is Created.

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

    but there is two Constructor is getting invoked to initialize the Parent member variable. I think if you know the concept of super() keyword which invokes the Constructor of parent Class and Initialize the member Variable of parent Class.

    0 讨论(0)
  • 2020-11-29 19:44

    In Code only one object will be created and super call the parent class constructor .

    Prove of object creation :

    package one;
    
    public class A {
        public static A super_var;
    
        public A() {
            super_var = this;
            System.out.println("Constrcutor of A invoked");
        }
    }
    
    package two;
    
    public class B extends A {
        public static A sub_var;
    
        public B() {
            sub_var = this;
            System.out.println("Constructor of B invoked");
        }
    
        public void confirm() {
            if (sub_var == A.super_var)
                System.out.println("There is only one object is created");
            else
                System.out.println("There are more than one object created");
        }
    
        public static void main(String Args[]) {
            B x = new B();
            x.confirm();
        }
    }
    

    This will prove that there will be only one object created.

    And about Super(). What I know that it call Parent class constructor . and each constructor ahve Super() as first statement like you mention in your code . so that you know

    I don't know how it internally call super class constructor .

    Hope this will make you understand there is only the instace you create in program

    0 讨论(0)
  • 2020-11-29 19:44
    How many number of Object is created in this case.
    

    When you create an instance of Class C by C cInstance = new C(); a single instance(Object) of Class C is creates(None of A and B). However since C extends B and B extends A, C will have all the methods of Class A and B(Actually depends on access modifiers used but lets say for this case they are public or default).

    If one object is created then how internally Super() is calling Parent class Constructor
    . How Super is able to call parent class constructor.
    

    That is how inheritance works. When a new object is created it will call it's super class constructor and that super class will call it's super class constructor and so on. In other ordinary function you have to explicitly call super(). So calling super class constructor goes bottom-up fashion while execution goes top-down fashion of the inheritance hierarchy tree

    0 讨论(0)
  • 2020-11-29 19:45

    3 constructors will call

    Code:

    class A
    {
        A()
        {
            System.out.println("In A");
        }
    }
    
    class B extends A
    {
        B()
        {
            System.out.println("In B");
        }
    }
    
    class C extends B
    {
        C()
        {
            System.out.println("In C");
        }
    }
    
    public class InheritanceTest {
        public static void main(String args[])
    
    
    
        {
            C c1=new C();
        }
    
    }
    

    Output:

    In A

    In B

    In C

    0 讨论(0)
提交回复
热议问题