tags:

views:

127

answers:

6

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.

+8  A: 

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
"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"
CoffeeAddict
The object which is implementing the interface (I was assuming this was the one that you were writing).
Donnie
+1  A: 

Hi coffeeaddict... 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

ŁukaszW.pl
Yea I understand what they are....there is a lot more beyind my question but it's too much to explain.
CoffeeAddict
thanks for the good description about using a base class to implement interface members. That helps a lot.
CoffeeAddict
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?
CoffeeAddict
By outside world I mean outside of the class, not assembly ;)
ŁukaszW.pl
+2  A: 

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
Yea I understand that. So create an interface for your Abstract Classes is one option then.
CoffeeAddict
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
CoffeeAddict
+1  A: 

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
+1  A: 

Hi coffeeaddict,

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
A: 

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