When a subclass C is instantiated, it’s known that the constructors of its super classes (say A, and B (B extends A)) will be instantiated earlier than C. So does this mean that:
- There are separate memory allocated for A’s instance, B’s instance and C’s instance?
- For the instance of subclass
C, does it have all the physical memory allocated for the fields inherited fromBandA, in addition to its own fields? - and so does
B‘s instance has the physical memory for the fields inherited fromAin addition to its own?
Classes are instantiated. Constructors are invoked.
(It was suggested in an edit that this should be “Objects are instantiated”; however, this isn’t technically correct. Per the same JLS section linked below: “A new class instance is explicitly created when evaluation of a class instance creation expression causes a class to be instantiated.” I.e. you instantiate a class in order to get an object. An object is an instance. You don’t instantiate objects.)
No, there is an instance of the class. I.e. one piece of memory.
Yes: “Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden.”
As per 1, there is no “B’s instance” in this scenario. There’s only the one instance.
Source: 12.5. Creation of New Class Instances, JLS 7