In C#, if all interface members are public and abstract by default, why can't we use override in implementing class?

90 Views Asked by At

In C#, if all interface members are public and abstract by default, why can't we use override in implementing class? While we can use override in abstract classes.

interface IExample
{
    int Ara();
}
class Matematiksel : IExample
{
    
    public override int Ara()
    {
        return 3;
    }
}

CS0115 : Matematiksel.Ara() : no suitable method found to override. abstract class Example

{
    public abstract int Ara();
}
class Math1 : Example
{
    public override int Ara()
    {
        return 2;
    }
}

1st one does not work while the second one works just fine.

1

There are 1 best solutions below

0
LWChris On

Interface methods and abstract methods are two different concepts.

By defining a member in the interface IExample, you require that whatever class or struct implements IExample (or an interface that inherits from IExample) must have a member with a matching signature. For methods, the matching method can be abstract or virtual, but it must be public or an explicit implementation. At compile time, it is determined for each implementing class and struct which members are the ones that satisfy the requirement and are defined as the implementations. Interfaces have a few more options than abstract methods if they use generics in combination with in and out co- and contra-variant modifiers. For properties, an interface may require a get when the implementing property has get and set.

If you define an abstract member, that is a different type of requirement for inheriting classes. Most notably abstract members can be protected, and abstraction only exists for classes, since structs cannot be inherited to implement abstract members. The signature of an abstract method has well-defined types, co- and contra-variants do not exist in this context.

You can also define a virtual method. This tells the runtime that there may be a more specific method to call when calling the method from any level within the type hierarchy. Therefore calling a virtual method means the runtime has to look uo the "best" method in the virtual method lookup table of the type definition, and then call that one, independent of the variable type. However virtual methods are still actual methods that have a method body and can be called with base..

abstract and virtual are much more closely related concepts, with the difference being that abstract says "there is no actual method body to call down here".

For an interface variable, it was explicitly defined which method would be called and that mapping takes precedence. For abstract and virtual, the runtime has to look up the actual method to call.

The override keyword is only used for abstract and virtual cases, and not for interface. It refers to overriding actual method definitions down in the inheritance hierarchy, whereas an interface is merely a mold that a given class or struct has to fit into, and where the interface members are wired to the actual member that is regarded as the implementation at compile time.

Could C# have been designed that you also must write override when you implement an interface method? Yes. But it's simply not how they defined the language.