Reputation: 12315
I know that the access specifier visibility of the overridden method/property must be the same or above as that of the base method/property and I also know that the visibility of members of interfaces is public by default. So how and why can the visibility of the members be private when we implement the interface explicitly in a class though those private methods can be accessed by casting the class object to the implemented interface?
public interface IRocker
{
string RockOff(string str);
}
public abstract class AmAbstract
{
public abstract string SomeMethod(string str);
}
class Program : AmAbstract, IRocker
{
static void Main(string[] args)
{
new Hello().RockU();
Console.ReadKey();
}
string IRocker.RockOff(string str)
{
return str;
}
public override string SomeMethod(string str)
{
return str;
}
}
public class Hello
{
public void RockU()
{
Console.WriteLine(((IRocker)(new Program())).RockOff("Damn"));
Console.WriteLine(((AmAbstract)(new Program())).SomeMethod("lalalalala"));
}
}
'Console.WriteLine(((IRocker)(new Program())).RockOff("Damn"));' statement shows us ultimately accessing a private method of the class 'Program'. My question is why we can't hide the visibility of an abstract method as we can with an interface method? And why we are able to access private method through interface? Any help will be highly appreciated.
Upvotes: 1
Views: 260
Reputation: 1021
You are using explicit interface implementation when implementing IRocker.RockOff()
. There are separate rules for those in C#, and in fact these are kind of private and public at the same time.
A quote from paragraph 13.4.1 of the C# 4.0 specification:
Explicit interface member implementations have different accessibility characteristics than other members. Because explicit interface member implementations are never accessible through their fully qualified name in a method invocation or a property access, they are in a sense private. However, since they can be accessed through an interface instance, they are in a sense also public.
Explicit interface member implementations serve two primary purposes:
- Because explicit interface member implementations are not accessible through class or struct instances, they allow interface implementations to be excluded from the public interface of a class or struct. This is particularly useful when a class or struct implements an internal interface that is of no interest to a consumer of that class or struct.
- Explicit interface member implementations allow disambiguation of interface members with the same signature. Without explicit interface member implementations it would be impossible for a class or struct to have different implementations of interface members with the same signature and return type, as would it be impossible for a class or struct to have any implementation at all of interface members with the same signature but with different return types.
There are no such exceptions for abstract classes, so just the regular modifier rules apply.
Upvotes: 2