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.
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”.
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 .
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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With