Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to assign a base class object to a derived class reference with an explicit typecast?

Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?.

I have tried it and it creates a run-time error.

like image 625
Maddy.Shik Avatar asked Apr 08 '09 11:04

Maddy.Shik


People also ask

Can you cast a base class to a derived class?

C++ Explicit type conversions Base to derived conversion Likewise, a reference to base class can be converted to a reference to derived class using static_cast . If the source type is polymorphic, dynamic_cast can be used to perform a base to derived conversion.

Can a pointer to a base class can point to objects of a derived class?

Explanation: A base class pointer can point to a derived class object, but we can only access base class member or virtual functions using the base class pointer because object slicing happens when a derived class object is assigned to a base class object.

Can we create derived class object from base?

Answer: No. In general, When we create object of a class, its reference is stored in stack memory and object is stored in heap and address of object is assigned to class reference.

Can a base class object access the methods of derived class?

In general this is not possible.


2 Answers

No. A reference to a derived class must actually refer to an instance of the derived class (or null). Otherwise how would you expect it to behave?

For example:

object o = new object(); string s = (string) o; int i = s.Length; // What can this sensibly do? 

If you want to be able to convert an instance of the base type to the derived type, I suggest you write a method to create an appropriate derived type instance. Or look at your inheritance tree again and try to redesign so that you don't need to do this in the first place.

like image 190
Jon Skeet Avatar answered Sep 18 '22 17:09

Jon Skeet


No, that's not possible since assigning it to a derived class reference would be like saying "Base class is a fully capable substitute for derived class, it can do everything the derived class can do", which is not true since derived classes in general offer more functionality than their base class (at least, that's the idea behind inheritance).

You could write a constructor in the derived class taking a base class object as parameter, copying the values.

Something like this:

public class Base {     public int Data;      public void DoStuff() {         // Do stuff with data     } }  public class Derived : Base {     public int OtherData;      public Derived(Base b) {         this.Data = b.Data;         OtherData = 0; // default value     }      public void DoOtherStuff() {         // Do some other stuff     } } 

In that case you would copy the base object and get a fully functional derived class object with default values for derived members. This way you can also avoid the problem pointed out by Jon Skeet:

Base b = new Base();//base class Derived d = new Derived();//derived class  b.DoStuff();    // OK d.DoStuff();    // Also OK b.DoOtherStuff();    // Won't work! d.DoOtherStuff();    // OK  d = new Derived(b);  // Copy construct a Derived with values of b d.DoOtherStuff();    // Now works! 
like image 39
Michael Klement Avatar answered Sep 19 '22 17:09

Michael Klement