开发者

Implementing an Interface but changing a member to be private

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 us开发者_开发问答ing more Interfaces in my architecture these days so I'm not that well versed yet.


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.


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?


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.


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜