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

In Java, suppose we have two classes A and B , so B inherits A and A has three private fields and a constructor with three parameters:

 public class A { private int a ; private int b ; private int c ; public A(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } } 

and here is class B

 public class B extends A { public B() { super(1,2,3); } } 

Consider the following test class

 public class TestA { public static void main(String[] args) { A a = new A(1,2,3); B b = new B(); } } 

The question is, what is the ordered process on the heap that occurs when you create class A with private fields and inherit it from class B ? What happens on the heap when instantiating these two classes? How is memory allocated and how do classes interact in computer memory?

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

+9
java heap inheritance jvm instance
source share
1 answer

Class objects are loaded into a heap, like any other object. This object simply represents a class.

Oracle's Official Guide to Understanding Memory

and the good old java specs , you can read the entire document, how the class loader works, you will not find anything that "Classes are loaded into a heap." In addition, you can perform an initial search on the Internet for further refinement.

Class B will compile fine.

now your questions in their order:

What is an ordered process on the heap that occurs when class A is created with private fields and inherits from class B?

You cannot define your order before jvm, how it manages its order, private members are not inherited, but exist in the instance of the object in the parent (super).

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

What happens on the heap when instantiating these two classes?

Usually the sequence will look something like once you make instances of A and B

  • reference to the A.class object (after creating an instance of class A )
  • reference to the B.class object (after creating an instance of class B )
  • object instance variable block
  • block of variables A instances (only a, b, c)
  • a block of instance variables B (in this case there is not a single one)

however, each JVM implementation is free to choose how it distributes each of them.

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

When you call B()

 B b = new B(); 

he will choose super(1,2,3)

So what will happen after that? nothing values ​​passed to super(); are not passed to A(int a, int b, int c) , and then assigned to instance variables of A , but this does not mean that these private fields are now available for B .. you just passed the values ​​to the super class constructor, which is all.

- CHANGE -

If you want a much better understanding of the heap and the stack, see this question

- EDIT-2 -

If you spend some time learning this wiki , it has everything about the JVM, including its process on the heap and other memory structures.

- Ultimate EDIT-3 -

In the context of the OP comment on private members of the Super class

Take a look at this . Answers to this question will clear your confusion regarding inherited members and private members that are not available, since the sub class instance is an instance of the superclass, the instance has all the fields of the father class! Private members are not visible to this child! This is what the JLS specification you are talking about! They occupy their space inside the object. They are not visible to the child class, but they are inside this instance.

+4
source share

All Articles