7

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.

PositiveGuy
  • 46,620
  • 110
  • 305
  • 471

6 Answers6

12

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.

Donnie
  • 45,732
  • 10
  • 64
  • 86
  • "The point of interfaces is that they provide a contract that other objects can use to communicate with your object." What do you mean specifically by "your object" – PositiveGuy Jul 08 '10 at 17:33
  • The object which is implementing the interface (I was assuming this was the one that you were writing). – Donnie Jul 08 '10 at 17:42
  • It is worth mentioning that Microsoft violates this very rule themselves. If you look at the KeyCollection or ValueCollection classes that are part of the core generic dictionary, you will see they implement ICollection and yet many of the interface methods are marked private. Make of that what you will. – WiredWiz Jan 07 '15 at 17:56
2

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?

andy
  • 8,775
  • 13
  • 77
  • 122
  • Yea I understand that. So create an interface for your Abstract Classes is one option then. – PositiveGuy Jul 08 '10 at 17:24
  • I want a contract that other implementations we do have this same pattern so the interfaces I am creating are common functionality that are implemented differently per type of application we're building using these interfaces which makes perfect sense at least tome to use an Interface instead of an Abstract class because I'm creating a bunch of interfaces for web service wrappers to use across various 3rd party APIs – PositiveGuy Jul 08 '10 at 17:26
2

As interface does not has an Access Modifier, if you still want your method private in the class which is implementing that interface, you can Implement that interface EXPLICITLY.

In that way your class methods will be Private.

Samdrain
  • 441
  • 4
  • 13
1

You have to fully understand what interfaces are. In fact there are just descriptions of the expectations that outside world could have about the class members. It do not creates the member, it just informs that specified class have specified method to use in public scope. So, as you can see by interface you could only describe public members.

On the other hand if you want to declare some private members that are fixed or virtual you can use classic inheritance with the abstract base class. In this case you will make all methods that you want to implement in subclasses as abstract, and implement methods that you want to be defined in base class.

Hope this helps.. Regards

Łukasz W.
  • 9,538
  • 5
  • 38
  • 63
  • Yea I understand what they are....there is a lot more beyind my question but it's too much to explain. – PositiveGuy Jul 08 '10 at 17:28
  • thanks for the good description about using a base class to implement interface members. That helps a lot. – PositiveGuy Jul 08 '10 at 17:29
  • Well, not just the outside world. You can use Interfaces to create required structure and to keep a consistent structure in an application layer. Also, since you should always unit test via Interfaces, then you should have an Interface for each class....so it's not just code reuse here or exposing functionality to the outside world via an abstraction...there are many other reasons why you would use an interface if you're not exposing an interface layer to the outside world am I not correct? – PositiveGuy Jul 09 '10 at 13:33
  • By outside world I mean outside of the class, not assembly ;) – Łukasz W. Jul 26 '10 at 06:01
1

Interfaces are only good for public access. Internally, it would be strange for an object to refer to itself through an interface.

If you want to have private variables that you force an implementation of, you want to use an abstract class, and mark them as protected.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
0

Think a little about this - and you understand that this can not be done:

Interfaces are like a contact. all the public fields of the interface are parts of the contact.

So, you can't hide them in a subclass...
What would happen if someone were to upcast your class object to the interface's type ?

You'd probably want to change your design - may be split your interface in to two interfaces? or and interface and an abstract class? we need more details to know...

uvgroovy
  • 453
  • 3
  • 9