views:

266

answers:

4

I'm having the following classes:

class Base
{
    public virtual void Print()
    {
        Console.WriteLine("Base");
    }
}

class Der1 : Base
{
    public new virtual void Print()
    {
        Console.WriteLine("Der1");
    }
}

class Der2 : Der1
{
    public override void Print()
    {
        Console.WriteLine("Der2");
    }
}

This is my main method:

Base b = new Der2();
Der1 d1 = new Der2();
Der2 d2 = new Der2();

b.Print();
d1.Print();
d2.Print();

The output is Base, Der2, Der2.

As far as I know, Override won't let previous method to run, even if the pointer is pointing to them. So the first line should output Der2 as well. However Base came out.

How is it possible? How the override didn't work there?

+11  A: 

You've never actually overriden the Base version of Print(). You've only hidden it with a separate virtual method (named the same) in Der1.

When you use the new keyword on a method signature - you are telling the compiler that this is a method that happens to have the same name as a method of one of your base classes - but has no other relation. You can make this new method virtual (as you've done) but that's not the same as overriding the base class method.

In Der2 when you override Print you are actually overriding the 'new' version that you declared in Der1 - not the version is Base. Eric Lippert has an excellent answer to a slightly different question that may help you reason about how virtual methods are treated in the C# language.

In your example, when you call Print, you are calling it in the first case through a reference of type Base - so the hidden (but not overriden) version of Print is called. The other two calls are dispatched to Der1's implementation, because in this case, you've actually overriden the method.

You can read more about this in the MSDN documentation of new and override.

What you may have intended to do with Der1 (as you did with Der2) is to use the override keyword:

class Base 
{ 
    public virtual void Print() 
    { 
        Console.WriteLine("Base"); 
    } 
} 

class Der1 : Base 
{ 
    // omitting 'new' and using override here will override Base
    public override void Print() 
    { 
        Console.WriteLine("Der1"); 
    } 
} 
LBushkin
That's what he did with Der2.
GalacticCowboy
@GalacticCowboy - indeed, that was my point. That's probably what was intended in Der1 as well, but somehow the `new` keyword was slipped in, instead of `override`. I'll update my post to make that more clear.
LBushkin
Thank you. An wonderful answer, I fully understood how it works now.
iTayb
For what it's worth, situations like this - and the confusion they cause - are precisely why the compiler should *not* tell you by default, "Oh, just put 'new' on it..." (CS0108: http://msdn.microsoft.com/en-us/library/3s8070fc%28v=VS.80%29.aspx) because that is almost *never* what you actually want to do.
GalacticCowboy
+4  A: 

It's because Der1 does not override Print, it replaces it with a brand new method that happens to have the same name (this is caused by the use of the new keyword). So, when the object is cast to Base it calls Print in Base; there is no override to call..

Fredrik Mörk
A: 

override will replace the previous method, but as your Der1 class doesn't override Print() (it Shadows it, to use a VB-ism), then the most overriden verion of Base's Print() is called, which happens to be the version it defines

Rowland Shaw
A: 

As everyone has said the class Der1 is replacing Print() instead of overriding it. To see this in action you could base d1 and d2 to Base and then call the print method. It will then return Base.

((Base)d2).Print(); //Base
Matthew Whited