I'm new to C# and OOP, just question on explicit interface implementation. Below is the code:
interface definition:
interface IRun
{
void Run(); // implicitly public
}
class that implement IRun
class Car: IRun
{
public void Run() // have to use public here to match Interface definition
{
Console.WriteLine("Run");
}
}
so we know that the members of an interface never specify an access modifier (as all interface members are implicitly public and abstract), so in the Car class, we can't code like:
class Car: IRun
{
private void Run() //compile error, since it is public in interface, we have to math access modifiers
{
Console.WriteLine("Run");
}
}
But I don't understand why if we explicitly implemented interface member as:
class Car: IRun
{
void IRun.Run()
{
Console.WriteLine("Run");
}
}
and my textbook says explicitly implemented interface members are automatically private.
But isn't that access modifier doesn't match each other(one is public in IRun, the other is private in Car)? how come compiler doesn't throw an error?
P.S I can understand that 'private' access modifier is needed to solve name collision if multiple interfaces have the same method signature. why how come access modifiers can not be the same in original interface definition and implementation?