When you create the object B, let's say by calling the default constructor
B myObject = new B();
Then the JVM allocates an object with more or less:
- Enough memory for every field explicitly declared in B (usually around 4-8 bytes per field, but it varies a lot from types and the host system)
- Enough memory for every eventual field inherited by A and its ancestors
- Enough memory to contain a reference to the dispatch vector (which should be around 4-8 bytes too)
The dispatch vector is used by the compiler to store the address of every method that can be invoked on the given object and it depends on the class of the object rather than the instance of the object itself (every object B has the same interface after all!)
So you do NOT need to allocate A, because there's no separate object A. You aren't instancing 2 separate objects. When you create B you are creating a "specialized" version of A.. which it can be viewed as A with something more. So only B needs to be allocated (but keep in mind that B also has everything its ancestors have)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…