Reputation: 632
I have a problem with interfaces in C#. I made an interface called IPerson that needs a name and age. After that I created a class that inherits from that interface, so it needs a name and age. When I try to write these variables in the class, they can only be public and I don't understand why.
public interface IPerson
{
string name { get; set; }
int age { get; set; }
}
public class Person : IPerson
{
// If I try to make them private or protected, they don't work. and IPerson will be colored red. Only if they are public it's good, why ?
private string name { get; set; }
private int age { get; set; }
}
Upvotes: 0
Views: 1312
Reputation: 11
I understand this is an old ticket, but it seems like the author has mistaken the purpose of interfaces to be for defining class requirements when interfaces are actually intended to serve the purpose of being a way for outside data structures to interface with instances of the class implementing the interface. I believe this description clarifies why it is being asked in the first place; he wants to use the interface to define requirements, but interfaces display what is available from the implemented class to other objects instead.
(I was enlightened to this fact about interfaces only recently and have started using interfaces a whole lot more since I now understand their usefulness as such. The term "contract" now means a whole lot more to me. It makes more sense to me to describe it as an "agreement" though (since contracts are not always signed; in this case, the class offering the services (which are class members) agrees to offer the items described in the interface)).
Interfaces are like SLAs but for classes. You can also think of them as APIs but for specific classes instead of an entire backend application (the 'I' in "API" stands for "interface", so this concept clearly applies at both levels).
Upvotes: 0
Reputation: 115
As Max Play said:
An interface is a kind of contract that you state. Everything declared in the interface needs to be accessible in the type that inherits from said interface.
Everything you place in an interface, must be accessible in any instance of your implemented object, unless it is used within it an explicit implementation.
But if you still want to use a pattern where you want to choose which property can be private, public or protected, an option is to create an abstract class, instead of the interface
Upvotes: 1
Reputation: 6864
Perhaps what you want is to implement the interface explicitly...
e.g.
public interface IPerson
{
string Name { get; set; }
int Age { get; set; }
}
public class Person : IPerson
{
string IPerson.Name { get; set; }
int IPerson.Age { get; set; }
}
These members can only be accessible publicly via a reference to the interface.
i.e You can't do the following...
Person me = new Person();
Console.WriteLine(me.Name); // Won't compile
but you can do the following...
IPerson me2 = new Person();
Console.WriteLine(me2.Name); // Will compile
Upvotes: 3
Reputation: 4037
An interface is a kind of contract that you state. Everything declared in the interface needs to be accessible in the type that inherits from said interface. When you now declare a method as private, you can't access it from the other object which tries to enact the contract, resulting in the interface not being implemented correctly.
This is also the reason, why interfaces do not have accessor declarations. The properties, methods and events defined in the interface are required to be public, because otherwise accessing the class instance through the interface would not work.
In your example, you derive Person
from IPerson
. Now imagine, you use it like this:
IPerson c = GetCustomer();
where GetCustomer
is defined like this:
public IPerson GetCustomer() {
// internal code
}
All you have access to now, is the given values declared in the interface. This means that name
and age
are defined in the interface and are accessible.
Let's say, Person also declares some other properties, like IPerson parent { get; set; }
. In this case, the parent may be of type Person
or of any other type that inherits from IPerson. But you can always be sure that the object that gets assigned to this property derives from IPerson
and will have the properties name
and age
defined.
There is also no reason to declare an interface for private members. What would be the benefit your code has from that? Interfaces exist to connect objects that do not know much about the other type, except the interface. Think of it, as a power plug: You plug it into the socket, but it does not care where the power comes from. And likewise, the socket doesn't care where the power is going. It is just the case that both use an interface the other can interact with.
Using an interface in a class just for itself would be pointless, because there is no information that is hidden or needs to be abstracted within a class.
Upvotes: 2