Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Java suppose we have two classes A and B such that 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);
 }
}

We 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 in the heap that occurs when creating the class A with private fields and inheriting it by the class B? What happens in the heap when creating instances of these two classes? How does the memory allocation happen and how the classes interact in the computer memory ?

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

like image 878
pentanol Avatar asked Jan 07 '15 06:01

pentanol


People also ask

What happens if you inherit two classes in Java?

The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class. Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes.

What is inheritance in Java and how it works?

In Java, a class can inherit attributes and methods from another class. The class that inherits the properties is known as the sub-class or the child class. The class from which the properties are inherited from is know as the superclass or the parent class. In Inheritance, the properties of the base class is acquired by the derived classes.

What is a heap in Java?

In Java, a heap is a chunk of memory which is shared among all threads. In a heap, all class instances and the array is allocated. It is created when JVM starts-up.

Can a subclass inherit a private member of a class?

Private member inheritance: A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods (like getters and setters) for accessing its private fields, these can also be used by the subclass. Java IS-A type of Relationship.


1 Answers

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.

like image 136
Sharp Edge Avatar answered Oct 26 '22 19:10

Sharp Edge