none
How many instances and references are created for a Base-Sub Class RRS feed

  • Question

  • I have 2 concrete classes. Class A and B. Class B subclasses from Class A.

    How many instances (objects on the heap) and references from the stack to the heap objects are created for the following code:

    1. ClassB b = new ClassB();

    2. ClassA a = new ClassB();

    Thanks!

    Thursday, July 29, 2010 3:42 AM

Answers

  • Hi,

      Thanks for your post.

      1. ClassB b = new ClassB();

          There's an instance of ClassB on the heap and a variable b of type ClassB on the stack references it.

      2. ClassA a = new ClassB();

          There's an instance of ClassB on the heap and a variable a of type ClassA on the stack references it.

       If you want insight of the object layout of CLR, please take a look at the following article.

       Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by gaussrifle Thursday, July 29, 2010 2:00 PM
    Thursday, July 29, 2010 7:23 AM
  • Thanks Sam. The reason I asked this is because someone told me that for both cases 2 objects are created (instance of ClassA and ClassB). Since ClassB subclasses ClassA and both constructors are called. It turns out, from the IL that only the constructor of ClassB is called when creating a new instance and ClassA's constructor is only called as a member function of ClassB.

     

    • Marked as answer by gaussrifle Thursday, July 29, 2010 2:00 PM
    Thursday, July 29, 2010 2:00 PM

All replies

  • Hi,

      Thanks for your post.

      1. ClassB b = new ClassB();

          There's an instance of ClassB on the heap and a variable b of type ClassB on the stack references it.

      2. ClassA a = new ClassB();

          There's an instance of ClassB on the heap and a variable a of type ClassA on the stack references it.

       If you want insight of the object layout of CLR, please take a look at the following article.

       Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by gaussrifle Thursday, July 29, 2010 2:00 PM
    Thursday, July 29, 2010 7:23 AM
  • Thanks Sam. The reason I asked this is because someone told me that for both cases 2 objects are created (instance of ClassA and ClassB). Since ClassB subclasses ClassA and both constructors are called. It turns out, from the IL that only the constructor of ClassB is called when creating a new instance and ClassA's constructor is only called as a member function of ClassB.

     

    • Marked as answer by gaussrifle Thursday, July 29, 2010 2:00 PM
    Thursday, July 29, 2010 2:00 PM
  •   2. ClassA a = new ClassB();

          There's an instance of ClassB on the heap and a variable a of type ClassA on the stack references it.

       If you want insight of the object layout of CLR, please take a look at the following article.

    As per my understanding stack contains reference to object of class B which is on heap. The object will have a Type Object pointer which will point to ClassB.

    How does variable 'a' contains information that it is of Type classA ?

    Friday, July 30, 2010 1:35 PM