By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

interface - why does implementation have to be public?

P: n/a
Below I've created an interface ... why do all implementations of the
methods have to be public? What if I want them to be private or protected?

public interface IOisWebPageStandard
{
void SomeMethod1();
void SomeMethod2();
void SomeMethod3();
}

Thanks!
Mark
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <e$**************@TK2MSFTNGP09.phx.gbl>,
mf****@idonotlikespam.cce.umn.edu says...
Below I've created an interface ... why do all implementations of the
methods have to be public? What if I want them to be private or protected?

public interface IOisWebPageStandard
{
void SomeMethod1();
void SomeMethod2();
void SomeMethod3();
}


What's the point in having an interface (a "contract" between two
parties) if you don't want the other party to be able to see the method?
The goal of an interface is to define how the object can be accessed by
the outside world.

Private/protected stuff is an internal implementation detail to the
class and has no bearing on those that use the class.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 15 '05 #2

P: n/a
Mark,
Below I've created an interface ... why do all implementations of the
methods have to be public? What if I want them to be private or protected?


You can use explicit implementation to have private methods

void IOisWebPageStandard.SomeMethod1() { ... }

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #3

P: n/a
A bit of clarification.

Explicit interface implementation is about visibility, not about
accessibility. If you implement something explicitly, it doesn't show up as
a method on the class, but you can still access it through the interface.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:Or**************@TK2MSFTNGP11.phx.gbl...
Mark,
Below I've created an interface ... why do all implementations of the
methods have to be public? What if I want them to be private or
protected?
You can use explicit implementation to have private methods

void IOisWebPageStandard.SomeMethod1() { ... }

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 15 '05 #4

P: n/a
Eric Gunnerson [MS] <er****@online.microsoft.com> wrote:
A bit of clarification.

Explicit interface implementation is about visibility, not about
accessibility. If you implement something explicitly, it doesn't show up as
a method on the class, but you can still access it through the interface.


While I don't agree with the OP's idea of interface methods being
private or protected, it *would* make sense to allow an internal
interface, allowing code such as:

using System;

internal interface Foo
{
int X();
}

public class Test : Foo
{
internal int X()
{
return 0;
}

static void Main()
{
}
}

Strangely enough, change X in Test to be public rather than internal
works - the interface itself can be internal, but it can't be
implemented internally.

What's the reason for this?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

P: n/a
Jon,

IIRC, we did this so as not to complicate the way interface implementation
works.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Eric Gunnerson [MS] <er****@online.microsoft.com> wrote:
A bit of clarification.

Explicit interface implementation is about visibility, not about
accessibility. If you implement something explicitly, it doesn't show up as a method on the class, but you can still access it through the
interface.
While I don't agree with the OP's idea of interface methods being
private or protected, it *would* make sense to allow an internal
interface, allowing code such as:

using System;

internal interface Foo
{
int X();
}

public class Test : Foo
{
internal int X()
{
return 0;
}

static void Main()
{
}
}

Strangely enough, change X in Test to be public rather than internal
works - the interface itself can be internal, but it can't be
implemented internally.

What's the reason for this?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.