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.
Interface methods and abstract methods are two different concepts.
By defining a member in the interface
IExample, you require that whatever class or struct implementsIExample(or an interface that inherits fromIExample) 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 withinandoutco- and contra-variant modifiers. For properties, an interface may require agetwhen the implementing property hasgetandset.If you define an
abstractmember, 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
virtualmethod. 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 withbase..abstractandvirtualare much more closely related concepts, with the difference being thatabstractsays "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
overridekeyword is only used forabstractandvirtualcases, and not forinterface. 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
overridewhen you implement an interface method? Yes. But it's simply not how they defined the language.