Why no public qualified interface implementations?

J

Jeff Louie

Can anyone explain why a interface method implementation using the fully
qualified name cannot be public or protected? Sample below:

public interface IDrawable
{
void DrawYourself();
}
public interface IPositional
{
Point Position
{
get;
set;
}
}
public interface IShape : IDrawable, IPositional {}
public abstract class AbstractShape : IShape
{
Point position= new Point(0,0);
public virtual void DrawYourself()
{
System.Console.WriteLine("Abstract Shape");
}
// This cannot be public or protected
Point IPositional.Position
{
get {return position;}
set {position= value;}
}
// This works as expected in subclass
public Point Position
{
get {return position;}
set {position= value;}
}
}

Regards,
Jeff
 
A

Adrian Forbes [ASP MVP]

Interfaces are assumed public which is why you can't supply your own access
modifier. Think about it, what good is a non-public function on an
interface?
 
G

Guest

Interface members are public. You cannot implement the members with a
different access modifier.
 
J

Jeff Louie

Adrian and Rakesh.... So I assumed, but every time I try to subclass
AbstractShape using fully qualified names I find that an implementation
of a
method or property with a fully qualified name is _not_ touchable from
within
the subclass.

Regards,
Jeff
Interfaces are assumed public which is why you can't supply your own
access modifier. Think about it, what good is a non-public function on
an
interface?<
 
J

Jon Skeet [C# MVP]

Jeff Louie said:
Adrian and Rakesh.... So I assumed, but every time I try to subclass
AbstractShape using fully qualified names I find that an
implementation of a method or property with a fully qualified name is
_not_ touchable from within the subclass.

It is - but only if you cast it to the interface first.

From the C# spec:

<quote>
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.
</quote>
 
J

Jeff Louie

Hi Jon... Thanks for the clarification. This had me quite befuddled.

Regards,
Jeff

<quote>
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.
</quote>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top