2
class MyBaseClass
{
  virtual public void Print()
    {
      Console.WriteLine("This is the base class.");
    }
}

class MyDerivedClass : MyBaseClass
{
  override public void Print()
   {
     Console.WriteLine("This is the derived class.");
   }
 }

class Program
  {
    static void Main()
    {
      MyDerivedClass derived = new MyDerivedClass();
      MyBaseClass mybc = (MyBaseClass)derived;

      derived.Print();
      mybc.Print();

    }
   }

OUTPUT:

This is the derived class.
This is the derived class.

I do not understand why second call prints derived class's print() method because I cast mybc object to base class. I expect it to print base class print method instead. Am I missing something here?

Lyrk
  • 1,936
  • 4
  • 26
  • 48
  • Check out a similar asked question on this site for a thoughtful discussion: http://stackoverflow.com/questions/1334254/how-can-i-call-the-base-implementation-of-an-overridden-virtual-method – Steven Hansen Oct 05 '15 at 08:40
  • remove override keyword and all will work fine –  Oct 05 '15 at 08:40
  • @PranavPatel It would work in a technical sense, but generally that would be a bad design. – Matthew Watson Oct 05 '15 at 08:47

3 Answers3

4

The variable type and the instance type are two different types. Casting does not change the instance type.

When you declare a method to be virtual/abstract, you're saying that you want the instance type to determine behavior when called.

Also note that this assignment is valid - cast syntax is not needed to change variable type from subclass to baseclass. This kind of cast can be done implicitly.

MyBaseClass mybc = derived;
Amy B
  • 108,202
  • 21
  • 135
  • 185
  • Beware c# cast syntax, which sometimes casts and sometimes converts. – Amy B Oct 05 '15 at 08:44
  • When I cast a derived object into base object, like in the code above, do I change the object type or not? For example in this code piece, mybc became a MyBaseClass object or is it just a pointer to a derived object? MyBaseClass mybc = (MyBaseClass)derived; – Lyrk Oct 05 '15 at 08:51
  • You do not. You are saying: "Use this Shape variable to refer to my Square", but it is still a Square. – Amy B Oct 05 '15 at 08:52
  • Pls correct me. I made a pointer to a base class object and this pointer("mybc") got the address of a derived class object which is explicitly casted to its base class. Now what does mybc point to? A derived object or base object? (I have a C background so having hard time to understand this concept) – Lyrk Oct 05 '15 at 08:59
  • mybc is a variable that references an instance of MyDerivedClass. Don't confuse casting and converting. Converting makes a new instance of the specified type. Casting tells the compiler that it's ok that this reference points to that instance. – Amy B Oct 05 '15 at 09:27
3

You have overridden it. It only calls the derived method. You have to explicitly call the base class' method:

override public void Print() {
     base.Print();
     Console.WriteLine("This is the derived class.");
}
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Of course, you can only call the base version from inside the overriding class. You can't call the base version from outside the class. – Matthew Watson Oct 05 '15 at 08:41
2

The whole point of overriding virtual methods is that the version for the underlying (runtime) type of the object is called, rather than the one for the static (compile-time) type - even when you call it through a type declared as the base class.

So this is behaving exactly as it should.

If this was not the case, it would make a lot of the utility of a class hierarchy useless, because you couldn't change the behaviour of a class type passed to a method by passing it a customised derived class.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276