In C# .NET, I have 2 concrete classes. Class A and B. Class B is a subclass of Class A.
How many instances (objects on the heap) and references from the stack to the heap objects are created for each line of code:
-
ClassB b = new ClassB(); -
ClassA a = new ClassB();
Going with the analogy that the object is a balloon and the reference is a string that is tied to the baloon, in each of the following cases there would be one balolon and one string:
Running both at the same time will therefore create two objects and two references.
EDIT Have a look at this IL generated from
ClassBconstructor:call instance void InheritanceTest.ClassA::.ctor()indicates that it callsClassAconstructor as a member function(not as a function on a member object). This is in line with my understanding about what happens with instances of inherited classes, that the derived class is simply all the members of the base class, followed by members of its own, similarly to C++.