Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning derived class object to a parent class reference

I am always puzzled when I see:

Parent ref = new Child();

where Child class extends Parent.

  1. How does the object ref look like in memory?
  2. How is virtual method treated? non-virtual?
  3. How is it different from:
Child ref = new Child();
like image 710
Ting Yun Avatar asked Dec 05 '11 19:12

Ting Yun


People also ask

What happens when a derived class is assigned to a reference to its base class?

Moreover, Object slicing happens when a derived class object is assigned to a base class object, and additional attributes of a derived class object are sliced off to form the base class object.

What happens when a reference to a derived class object is assigned to a base class reference variable in C#?

when we create a child class object,the base class object is auto initiated so base class reference variable can point to child class object. but not vice versa because a child class reference variable can not point to base class object because no child class object is created.

Why would one create a base class object with reference to the derived class?

One reason for this could be that BaseClass is abstract (BaseClasses often are), you want a BaseClass and need a derived type to initiate an instance and the choice of which derived type should be meaningful to the type of implementation.

Can we create a base class object from derived class C#?

In C#, both the base class and the derived class can have their own constructor. The constructor of a base class used to instantiate the objects of the base class and the constructor of the derived class used to instantiate the object of the derived class.


2 Answers

How does the object look in memory?

Your question is unclear. There are two relevant memory locations. The variable is associated with a storage location. That storage location contains a reference to another storage location.

The variable's storage location is typically realized as a four or eight byte integer that contains a "managed pointer" -- a memory address known to the garbage collector.

The object's memory layout is also an implementation detail of the CLR. The memory buffer associated with the object will contain all the data for the object -- all the values of the fields and whatnot. It also contains a reference to yet another memory location, the virtual function table of the object.

The virtual function table (vtable) then contains even more references, this time references that refer to the methods associated with the most-derived type of the object.

How is virtual method treated? non-virtual?

Virtual methods are executed by looking up the object reference from the variable, then looking up the vtable, then looking up the method in the vtable, and then invoking that method.

Non-virtual methods are not invoked via the vtable because they are known at compile time.

How is it different from...

Non-virtual methods called on the object will call the version of the method based on the type of the variable. Virtual methods called on the object will call the version of the method based on the type of the object that the variable refers to.

If that is not all clear, you might want to read my article that explains how you might "emulate" virtual methods in a language that does not have them. If you can understand how to implement virtual methods yourself in a language that does not have them, that will help you understand how we actually do implement virtual methods.

http://blogs.msdn.com/b/ericlippert/archive/2011/03/17/implementing-the-virtual-method-pattern-in-c-part-one.aspx

like image 142
Eric Lippert Avatar answered Sep 22 '22 06:09

Eric Lippert


ref is a Child object. virtual methods are called on Child class. However, methods defined only in Child class are not visible when assigned to Parent object.

If foo() was not virtual, then the compile will select a method based on the declared type of the variable ref. If you have Parent ref = new Child(); then Parent.foo() will be called. If you have Child ref = new Child(); then Child.foo() will be called. Of course, in this case the C# compiler will ask you to use new in the declaration of Child.foo() to indicate that you mean to hide the implementation in Parent.

like image 20
drdwilcox Avatar answered Sep 24 '22 06:09

drdwilcox