What happens in the heap when class A inherits class B in Java

前端 未结 1 770
暗喜
暗喜 2021-01-11 23:59

In Java suppose we have two classes A and B such that B inherits A and A has three private fields and a cons

相关标签:
1条回答
  • 2021-01-12 00:28

    Class objects are loaded into the heap like any other object. This object just represents the Class.

    Oracle official 'Understanding Memory guide'

    and good old java specs , you can read the whole document as how the class loader works, you won't find anything that "Classes are loaded in Heap" .. Further more you can do some initial search on the internet for further clarification.

    Class B will compile perfectly.

    now your Questions by their order:

    what is the ordered process in the heap that occurs when creating the class A with private fields and inheriting it by the class B?

    You cannot determine their order its up to jvm as how it manages their order, private members are not inherited but exist in the instantiated object in the parent (super).

    In other words, Yes, instances of the subclass will have copies of a private field of the parent class. They will however not be visible to the subclass, so the only way to access them is via methods of the parent class.

    What happens in the Heap when creating instances of these two classes?

    Typically the sequence will be something like after you make instances of A and B

    1. reference to the A.class object (after making Aclass instance)
    2. reference to the B.class object (after making B class instance)
    3. block of Object instance variables
    4. block of A instance variables (only a,b,c)
    5. block of B instance variables (none in this case)

    however each implementation of a JVM is free to choose how it allocates each of them.

    We know also that a subclass cant inherit the private fields of its superclass, so what happens exactly when the constructor B() is called?

    When you Call B()

    B b = new B();
    

    it will call the super(1,2,3)

    So what will happen after that ? nothing the values passed to super(); are passed to A(int a, int b, int c) and then assigned to the instance variables of A but this doesn't mean that those private fields are now accessible to B.. you just passed values to super class constructor thats all.

    --EDIT--

    If you want a much better understanding of of Heap and Stack, see this question

    --EDIT-2--

    If you take some time out to study this wiki , it has everything about JVM including its process in Heap and other memory structures

    --Final EDIT-3--

    In context of OP's comment regarding private members of Super class

    Take a look at this The answers of this question will clear your confusion regarding inherited members and not accessible private members, since sub class instance is the instance of super class, it's instance has all the fields of father class ! Private members are not visible to that child !! Thats what JLS specification you're referring to! They take up their space inside the object. They are not visible to child class but they are there inside that instance.

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