1

I'm wondering about how objects are represented in memory, and how the supertype portions of an object are actually organized and related, under the hood of the JVM.

If A extends Object and B extends A, and we execute: new B(), does this amount to the JVM making an Object instance, an A instance (with only members of A itself), and a B instance (with only members of B itself), and then linking them all together somehow ultimately to make a new B object including all members of B and its supertypes? I'm aware at least that constructor calls are chained in order of inheritance.

Could you also clarify how typecasting references is integrated with the representation of objects in memory?

And how does this work for abstract classes and interfaces as supertypes?

What I described above is basically how I picture it for myself when coding, but I'd like to know about how this is actually implemented in memory by the JVM. I realize I may have some misconceptions here, so feel free to correct me where necessary. Thanks!

  • 1
    That is a very big question. I'm not sure if it's too broad for this site or not, but you could spend years researching how to represent best OOP objects in memory. – Silvio Mayolo Jun 24 '19 at 23:09
  • It depends on the implementation of the JVM, but I believe most systems will only a create single reference. Usually this means when the object is allocated that the JVM will allocate space for a reference to each instance variables required by each class in the inheritance tree, and constructs virtual lookup table for resolving method calls. – flakes Jun 24 '19 at 23:09
  • It can't create a separate object for each supertype. Othewise you would have multiple inheritance of the supertype for example, and of `Object` if different. A single object is constructed with the data members of the supertypes in it as well. – user207421 Jun 24 '19 at 23:25
  • The Java specification explicitly doesn't address internals like this; the JVM author is free to use any correct method. If you are interested in the most common case, you might want to ask about HotSpot specifically. – chrylis -cautiouslyoptimistic- Jun 24 '19 at 23:42

0 Answers0