none
why base class object cann't assign to derived class?

    Question

  • Can any one help me?? Please related with stack and heap .
    • Moved by Paul Zhou Thursday, January 12, 2012 6:13 AM (From:.NET Platform Architecture Development Discussions)
    Tuesday, January 10, 2012 6:46 PM

Answers

  • value types are stored in stack and reference types are stored in heap.  right??


    If a value type is a member of an object it'll be stored on the heap too. But value types vs. references types have nothing to do with your question, neither does heap vs. stack.

    Imagine that the A object is a block. The B object is a block too, but it's an A block with a little B unique block added on one side. Visualizing it like that helps me.

    For your example, the reason why A aobj = new B(); works is because later in the code when something wants to work with aobj, all of the A members are there (ie. firstInt). The reason why B bobj = new A(); doesn't work is because later on in the code when something wants to work with bobj, not all of the B members are there. Since bobj is being declared as being of type B that's saying "I will have all of the members of B, and take up the amount of member that B takes up". But an A object takes up less memory than a B object, and it doesn't have all of the members. Imagine that the compiler let the second example work. Then later in the code you have the statement bobj.firstString; what's the computer supposed to do? bobj is of type B, so the object should have a firstString field, but since the memory declared for the object is laid out as an A object, there is no firstString object! There are languages which do allow for that type of behavior (languages which aren't type safe), but C# is a type safe language and does not allow for that.

    Wednesday, January 11, 2012 5:17 PM

All replies

  • Because an instance of the base class doesn't have all of the fields/properties of the derived class.
    • Proposed as answer by Tiya01 Wednesday, January 11, 2012 7:18 AM
    Tuesday, January 10, 2012 8:41 PM
  • Hi Jared,

     

    Thanks for your reply.

    objects are reference types.

    value types are stored in stack and reference types are stored in heap.  right??

    can you please give me a information about stack and heap here? I mean how stack and heap will work on the following example?

    while creating object  A aobj = new B(); -> works fine

    but                             B bobj = new A();  -> ????????

    Ex:

    Class A{

    int firstInt = 10;

    }

    Class B : A

    {

    string firstString = "Welcome";

    }

     

     

    Wednesday, January 11, 2012 6:49 AM
  • Hi alavudeen batcha,

    In your case object creation is not related to heap or stack.

    Jared Van Leeuwen said it right.

    Explanation :

    Consider yourself and your parent.

    Your parents know every mischief done by you but in turn you do not know all the mischiefs done by your parent.

    You only know which were explicitly told to you.

    Same scenario is with class A and B.

     

    Hope this helps!

    Thanks


    Every day its a new learning. Keep Learning!!
    If this post answers your question, please click Mark As Answer . If this post is helpful please click Mark as Helpful
    Wednesday, January 11, 2012 7:19 AM
  • Hi Tiya01,

     

    Thanks for your reply. I understood but still I have one doubt, see the below link.

    http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory2B01142006125918PM/csharp_memory2B.aspx


    Wednesday, January 11, 2012 4:13 PM
  • value types are stored in stack and reference types are stored in heap.  right??


    If a value type is a member of an object it'll be stored on the heap too. But value types vs. references types have nothing to do with your question, neither does heap vs. stack.

    Imagine that the A object is a block. The B object is a block too, but it's an A block with a little B unique block added on one side. Visualizing it like that helps me.

    For your example, the reason why A aobj = new B(); works is because later in the code when something wants to work with aobj, all of the A members are there (ie. firstInt). The reason why B bobj = new A(); doesn't work is because later on in the code when something wants to work with bobj, not all of the B members are there. Since bobj is being declared as being of type B that's saying "I will have all of the members of B, and take up the amount of member that B takes up". But an A object takes up less memory than a B object, and it doesn't have all of the members. Imagine that the compiler let the second example work. Then later in the code you have the statement bobj.firstString; what's the computer supposed to do? bobj is of type B, so the object should have a firstString field, but since the memory declared for the object is laid out as an A object, there is no firstString object! There are languages which do allow for that type of behavior (languages which aren't type safe), but C# is a type safe language and does not allow for that.

    Wednesday, January 11, 2012 5:17 PM
  • Hi,

    I have gone thought the provided link.

    In this the author has explained how reference types are passed between methods.

    Don get confused with the two things.

    Inheritance is different and passing parameters as value or reference types is diff.

    Thanks!


    Every day its a new learning. Keep Learning!!
    If this post answers your question, please click Mark As Answer . If this post is helpful please click Mark as Helpful
    Thursday, January 12, 2012 4:18 AM
  • Thanks Jared and thanks a lot for your clear and simple example.
    Thursday, January 12, 2012 11:21 AM
  • Hi,

    I am adding few points to the replies

    1) Base class doesn't have any idea about its child class, the next question that flashed immediately is how the base class storing the child class object without knowing any thing? right?

    The answer is this -

    1.) Base obj = new Child();

    In the above code we are creating the child object, for which the memory is going to be allocated on the heap. The reference to the heap is stored on the stack. As the Child class is a specliaziation of Base class. It is storing the reference.

    Thanks

    Thursday, January 12, 2012 2:51 PM