简体   繁体   中英

Why do a child class need to implement a parents interface to hide one of that parents methods in C#?

I found this behaviour while working with a third-party library where I needed to hide and change one of it's methods.

I have the following setup:

interface IBaseInterface
{
    string MethodToHide();
}

class BaseClass : IBaseInterface
{
    public string MethodToHide()
    {
        return "BaseClass";
    }
}

class ChildClass : BaseClass
{
    new public string MethodToHide()
    {
        return "ChildClass";
    }
}

Why is it that when I run the following:

var i = (IBaseInterface) (new ChildClass());
Console.WriteLine(i.MethodToHide());

the output is

BaseClass

, but when changing the ChildClass signature to

class ChildClass : BaseClass, IBaseInterface

, the output is

ChildClass

Why do I need to explicitly specify the interface for the BaseClass method to be hidden by the ChildClass?

You need to read a bit more about the difference between overriding and hiding :

Link: Override versus Hide

In a nutshell:

Hiding (using the new) runs the method according to the variable's type.
Overriding overrides the method and will only use the child's method.

Edit :

When you use an Interface variable:

var i = (IBaseInterface) (new ChildClass());

The compiler will search for the best match for the methods that are used by the interface.
Because you declared the BaseClass to implement the interface, then its methods will be chosen.

If the ChildClass doesn't explicitly implement the interface, then the compiler can't link the methods to the interface. By its point of view the BaseClass just happens to have methods by the same name.

When you explicitly declare that the ChildClass also implements the interface, then its methods will be the best choice.

You need to mark the MethodToHide as virtual in your BaseClass. Then your ChildClass can override it. See http://msdn.microsoft.com/en-us/library/9fkccyh4(v=vs.80).aspx for more details.

I believe this is the relevant part of the spec:

10.6.3 Virtual Methods In a virtual method invocation, the run-time type of the instance for which that invocation takes place determines the actual method implementation to invoke. In a non-virtual method invocation, the compile-time type of the instance is the determining factor.

So when you cast your original object to IBaseInterface. It determines that the type is BaseClass since that's the one that implements it and calls its method. In the second example it resolves it to ChildClass and calls its method.

Because the main-class implements the interface. The child-class just defines a new method, which has nothing to do with the interface. In fact the child class hides the method - that's what 'new' does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM