Implementing an interface, but changing a member for private

All participants in the interface are publicly accessible by default. But there are some properties in my interface that I want to use as private members of some subclasses that implement my interface. Is this something that can and is done, or I do not agree with this. I am working on using more interfaces in my architecture these days, so I'm not as good at it yet.

+7
c # interface
source share
6 answers

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

An interface will never have private members, since the interface is designed to "pair" between two objects. Your internal private members do not matter if you delay your end of the contract.

+12
source share

Going to your question and using the word "subclass", I donโ€™t think you still understand the interfaces.

I know that you probably heard it a million times, but the interface describes what the object does, and the class HOW it does it. IMPLEMENTS class, interface, it DOES NOT INCLUDE from it.

So, if you want, you have an interface for the base class or for your subclasses, but your question makes me think that you are thinking of a base class (abstract class), not an interface.

It makes sense?

+2
source share

Since the interface does not have an access modifier, if you still want your method to be private in a class that implements this interface, you can implement this interface EXPLICITLY.

That way your class methods will be Private.

+2
source share

You must fully understand which interfaces. In fact, there is only a description of the expectations that the outside world may have about the members of the class. It does not create a member, it simply reports that the specified class indicated the specified method for use in the public domain. So, as you can see by the interface, you can only describe public members.

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

Hope this helps .. Regards

+1
source share

Interfaces are only for sharing. Inside, it would be strange if an object referred to itself through an interface.

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

+1
source share

Think about it a bit and you realize that this cannot be done:

Interfaces are like contact. All public interface fields are parts of the contact.

This way you cannot hide them in a subclass ...
What happens if someone raises your class object to an interface type?

You might want to change your design - maybe you divided your interface into two interfaces? or interface and abstract class? we need to know more details ...

0
source share

All Articles