So this has me perplexed.
Suppose two interfaces.
public interface a
{
void foo();
}
public interface b
{
void foo();
}
Both of those interfaces have a function foo, I have a class that provides explicit implementation:
public class alpha : a, b
{
// why can't I put an access modifier here?
// How would you be able to hide this from a derived class
void a.foo()
{
Console.WriteLine("a");
}
void b.foo()
{
Console.WriteLine("b");
}
}
And a Class that is derived from alpha
public class beta : alpha
{
}
How do you make foo private or protected since alpha doesn't allow access modifiers on explicit imlementation, what can stop someone from calling:
var be = new beta();
(be as b).foo();
EDIT
How come when I don't explicitly provide implementation I can provide an access modifier?
public class alpha : a, b
{
//why this compile?
public void foo()
{
Console.WriteLine("both");
}
}
Since interface a
is public, any class that implements a
must make the methods of a
publicly accessible, either implicitly (through public methods) or explicitly. Explicit implementations are "sort-of" private since they can only be accessed through the interface.
In short, there is no way to completely "hide" foo
- your class implements both a
and b
so those methods must me made accessible through some means.
This would be true even if you only had one interface - having multiple interfaces with a method name collision just forces you to make the implementations explicit. If you had one interface, foo
would either have to be public
or explicit.