C# interface method declared in Base class need not be again implemented in Derived class

Jasmine

It could be a silly question, but I am learning, and I was just curious what's happening, today I was playing with few oops concepts and learning it in VS. I was again puzzled to see that we don't have to implement multiple interfaces same method into a derived class where ACTUALLY we "inherit" the interface, but in base class.

May I know how it works? My concern is, even though I do not "inherit" interface methods in base class, I use a method with same name. I also do not implement it in derived class.

Can somebody help me understand what's happening and how and why?

Class A
{
    public void Display()
    {
        Console.Writeline("I am from A");
    }
}

interface IA
{
    void Display();
}

interface IB
{
    void Display();
}

Class B : A, IA, IB
{

}

Class Final
{
    static void Main()
    {
        B b = new B();
        b.Display(); // displays class A Display method.
        Console.Readline();
    }
}
Dan Puzey

Although I can't speak for the language team, you can answer this question by posing the alternative solution.

You want to know why B is considered to implement the interface IA even though the required method definition is in base class A, which doesn't implement the interface. So, let's consider the opposite: B should not be considered to implement the interface because the base class' method wasn't written with that interface in mind.

This means that your code doesn't compile. Why doesn't it compile? Because B doesn't implement required member Display of interface IA.

To fix this, you'd add a method Display to class B. That fixes the interface implementation. However, you now have a new compilation problem: you'll see a warning "B.Display()' hides inherited member 'ConsoleApplication1.A.Display()'. Use the new keyword if hiding was intended."

This is because your A.Display wasn't overrideable - and you don't want to override it. You can implement a method to call base.Display() if you choose, but this is extra code to essentially do nothing, and it makes a mess of your inheritance since a new method is handled differently to an override. (If you write A x = new B(); x.Display(); then you'll actually call A.Display() directly, which could get messy as your code evolves and is an accident waiting to happen.)

Alternatively, you might implement an entirely new B.Display method. What you've also now done is hidden the method implemented in class A from anyone who might derive from B or create an instance of B. Using new to hide methods is rarely a recipe for an understandable object structure, and this would be no exception - all so that you can implement an interface cleanly.

So ultimately, I would imagine, this decision was made because the alternative is far too messy.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Calling function that was declared virtual in interface (and implemented in derived class) inside base abstract class

override the return type of method declared in the interface after its implemented in class

returning derived class from overrided method, declared as returning base copy

c# implemented interface variable used in derived class

C#: my derived class cannot override base class's interface method implementation, why?

Using signature of implemented method on Derived class for inherited method from Base class in a "template method pattern" inheritance in Python

c++ - accessing derived class method via abstract template base class interface pointer, without explicit type in interface

Using a method declared in an interface, but not class

is it possible to allow an abstract method to be implemented in derived class, but only called in the base class?

C++ derived class calls method on base class before initialization

Cannot overload base class method in derived class in C++

c# base class method returns derived class

C++: Calling base class method on derived class member

Why can't I cast base reference to a variant interface implemented by derived class?

C++ Inheritance: Derived class pointer to a Base class invokes Derived class method

Do I need to define a same Interface in a derived class after define it in a base class?

Method in base class that returns derived class type?

Executing base class overridden method in derived class

Unable to access base class method in derived class

Clone derived class from base class method

Base class parameter in a derived class' method

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

Can an interface require a base class to be implemented?

Change base class fields in derived class with base class method

C# Method in base class that returns derived type

Calling a private base method from a derived class in C#

How to call base method from derived class instance in C#?

Use a derived class type as parameter of a base method in C++

Accessing to base class of derived class by derived class C++