9

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
pentanol
  • 330
  • 2
  • 7
  • 18
  • Class B will not compile. – BetaRide Jan 07 '15 at 06:39
  • @BetaRide it will compile – Sharp Edge Jan 07 '15 at 06:41
  • 1
    @BetaRide I know class B will not compile. I am not asking if it compiles or not. I am asking what happens in the level of the heap. I want to understand the relation between these classes in the memory level. – pentanol Jan 07 '15 at 06:41
  • 1
    Although a subclass doesn't "inherit" the private fields of the superclass, those fields still exist (and take space) in the object, and when a constructor for B calls the superclass constructor (whether implicit or not), those fields will at some point be initialized. And a method in B can call a superclass method on the same instance, and it has to be able to access the private fields in the superclass. "Not inheriting the private fields" just means the fields aren't visible from code in B. But they still are present. – ajb Jan 07 '15 at 06:49
  • @ajb You mean not visible by the code in B but accessible through the code in A. I am true ? – pentanol Jan 07 '15 at 06:57
  • Yes, I think you have the idea. – ajb Jan 07 '15 at 07:03
  • 1
    A lot of the details of *how* this is organised are highly implementation specific. The JLS specifies *what* must happen but not *how*. The answer you've accepted from sharp edge contains several things which are flat out wrong & should be unaccepted. For example, the JLS makes no mention of where classes should be loaded, and in Open/OracleJDK version 7 & below (one of the most popular JVMs on the planet) classes are loaded into the heap, albeit in a private area called permgen that user code can't touch directly. – kittylyst Jan 07 '15 at 13:16
  • For detail on how Open/Oracle does it you want to look for OOPs (Ordinary Object Pointers) and the terms markoop and klassoop. – kittylyst Jan 07 '15 at 13:16
  • As a start yes, If you look at my answer @kittylyst I've mentioned that its jvm dependent as how its handles memory allocation. Secondly from a beginner's point of view Only the instance of a class is loaded in Heap not the entire class !! However, information about classes like the compiled code is loaded into the Permgen space. Class objects are loaded into the heap like any other object. This object just represents the Class. but the OP was asking about the instance of the class not the entire class so I failed to mention that. – Sharp Edge Jan 07 '15 at 13:42

1 Answers1

5

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.

Community
  • 1
  • 1
Sharp Edge
  • 4,144
  • 2
  • 27
  • 41
  • I am already asking about the JVM the process does. The process of the JVM is not arbitrary. It is organized. – pentanol Jan 07 '15 at 07:46
  • The process order of JVM does not matter as it can vary from one implementation of JVM to other.. The typical process of JVM is in my second answer in this answer :) – Sharp Edge Jan 07 '15 at 07:51
  • Thanks a lot me Sharp for this beneficail answer. But the question is already about the process of the JVM. – pentanol Jan 07 '15 at 07:53
  • 1
    Mr @Sharp edge, there is a kind of confusuion here. You say that private fields are inherited by the subclass and only accessible by the constructor of the superclass. But according to the [JAVA API](http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) they are not inherited at all. Can you show me a source that says that private fields are inherited by the sybclass ? – pentanol Jan 07 '15 at 08:24
  • 1
    @pentanol I never said that private members of super class are inherited in child class !! Please see my answer again, I said that OBJECTS of subclasses contain private fields of their superclasses. The subclass itself has NO NOTION of private fields of its superclass. I know it sounds confusing but The child class object has super class members private or public, but the private members are not visible. Thats it – Sharp Edge Jan 07 '15 at 08:31