3

Possible Duplicate:
Is there a reason you can not define the access modifier on a method or in an interface?

Hello,

I'm curious about interfaces. Let's say I've the following interface's definition

public interface IPersone
{
  string FirstName { get; set; }
  string LastName { get; set; }
  int CalculateAge(int YearOfBirth);
}

Why there's no modifiers (public, private, protected) in front of methods and properties defining an interface? Is there any reason for that?

Thanks for helping

Community
  • 1
  • 1
Richard77
  • 20,343
  • 46
  • 150
  • 252

6 Answers6

7

Quote from MSDN: Interface members are always public because the purpose of an interface is to enable other types to access a class or struct.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
3

Members of an interface are always public.

An interface is a contract that defines methods and properties that you will always be able able to call on any object that implements the interface. Protected or private members would violate this contract.

Chris Shouts
  • 5,377
  • 2
  • 29
  • 40
3

An interface is a public contract for a class defining its interaction with the outside world. So everything in it is implicitly public.

David M
  • 71,481
  • 13
  • 158
  • 186
2

Since a class cannot inherit from an interface, only implement it, if you marked a method as private or protected there would be no way for a class implementing the interface to access these methods.

For what it's worth, if you need an "interface" to have protected and private members, you can use an abstract base class instead.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
1

http://en.wikipedia.org/wiki/Interface

In the first paragraph it says, "An interface is a point of interaction between two systems or work groups."

By default, anything defined in an interface should always be public, otherwise the whole concept of an interface is broken.

Ian P
  • 12,840
  • 6
  • 48
  • 70
0

An interface defines a public contract for an object. Protected/private methods, etc, are implementation details, not part of the object's public contract.

You might want to refer to Why can't I have protected interface members?.

Community
  • 1
  • 1
dsolimano
  • 8,870
  • 3
  • 48
  • 63