Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing an Interface but changing a member to be private

Tags:

c#

interface

All members of an Interface are public by default. But there are some properties in my interface that I want to be used as private members of some subclasses that implement my interface. Is this something that can and is done or am I way off basis here. I'm working on using more Interfaces in my architecture these days so I'm not that well versed yet.

like image 724
PositiveGuy Avatar asked Jul 08 '10 04:07

PositiveGuy


People also ask

Can interface members be private?

Therefore, the members of an interface cannot be private. If you try to declare the members of an interface private, a compile-time error is generated saying “modifier private not allowed here”.

Do interface members have to be public?

An interface can't contain instance fields, instance constructors, or finalizers. Interface members are public by default, and you can explicitly specify accessibility modifiers, such as public , protected , internal , private , protected internal , or private protected .

Can we implement interface in private section of any class?

Private interface methods can only be used inside interfaces. It is not possible to have both private and abstract modifiers at the same time. A static method can be used inside a static or non-static method. It is not possible to use a private non-static method within a private static method.

How do I hide members of an interface?

You can't. Interface members are always public... otherwise, the class would fail to implement the interface. That's why access modifiers are not allowed in interface member declarations. There are two ways of declaring members that satisfy the interface requirements: implicitly and explicitly.


2 Answers

The point of interfaces is that they provide a contract that other objects can use to communicate with your object. If you change a member which is declared as public in an interface to private then you're not fulfilling the contract - another object may need to read that property / call that method, and you must allow them to.

An interface will never have private members as an interface is for "interfacing" between two objects. Your internal private members don't matter to it as long as you hold up your end of the contract.

like image 99
Donnie Avatar answered Sep 19 '22 18:09

Donnie


Going on your question, and your use of the word "subclass", I don't think you've fully understood Interfaces yet.

I know you've probably heard this a million times but, an Interface describes what an object DOES, and a Class is HOW it does it. A Class IMPLEMENTS, an interface, it does not INHERIT from it.

So, if you want, have an Interface for you base Class, or for your SubClasses, but your question makes me think you're thinking about a base Class (Abstract Class), not an Interface.

Does that make sense?

like image 33
andy Avatar answered Sep 22 '22 18:09

andy