I am new to C# and I don't understand why compiler does not complain on this code. Here is the hierarchy of classes:
interface IAble
{
void f();
}
class AAble : IAble
{
public void f()
{
Debug.Log("---->> A - Able");
}
}
class BAble : AAble
{
public void f()
{
Debug.Log("---->> B - Able");
}
}
execution code:
IAble i = new BAble();
i.f();
On execution ---->> A - Able was printed. Why? How the compiler knows what function should be called?
When the decision is made of what function to call - runtime or compile time? What if I defile a new class class CAble : IAble?
Because
AAbleis implementing theIAbleinterface, itsAAble.fis marked as the implementation of theIAble.fmethod for typeAAble.BAble.fis simply hiding theAAble.fmethod, it is not overriding it.The decision is made at compile-time:
Interface implementations are marked as
virtualin IL, even though it wasn't marked virtual in C#. The method is also marked asfinalin IL, if the method would've beenvirtualin C#, it would not have been marked asfinal.