Jaxx
Jaxx

Reputation: 1575

Interface inheritance - hiding method from parent interface - why can I have both methods in the implementing class?

I am trying to understand how the inheritance of interfaces works. When an interface inherits another interface but has a method with the same signature, the compiler gives a warning that the base member is hidden (and this seems logical).

However, when I later implement the child interface in a class, I have to implement both the parent and the child interface method having the same signature and this confuses me... I thought that when the parent member is hidden I will need to implement only the child interface method?

(I came upon this while looking at IEnumerable and IEnumerable<T> interfaces)

interface IMyBase
{
    int DoSomething();
}

interface IMyDerived:IMyBase
{
    int DoSomething();
}


class myClass : IMyDerived
{
    int IMyDerived.DoSomething()
    {
        throw new NotImplementedException();
    }

    int IMyBase.DoSomething()
    {
        throw new NotImplementedException();
    }
}

Upvotes: 1

Views: 896

Answers (1)

nvoigt
nvoigt

Reputation: 77294

I have to implement both the parent and the child interface method having the same signature and this confuses me

So what do you expect will happen if you do this?

IMyBase x = new myClass();

x.DoSomething();

There is two methods, IMyBase.Something and IMyDerived.Something, both have to exist because both can be called. myClass still implements IMyBase, the interface was not hidden in any way.

Upvotes: 1

Related Questions