Last post May 26, 2013 11:42 PM by Paul Linton
May 22, 2013 04:45 PM|pradeepmanker|LINK
Why interface members are only public?? Why not protected or any other access specifiers we can use in interface members??
May 22, 2013 04:51 PM|Rion Williams|LINK
Basically interfaces act as a public API of sorts and allow you to see what kind of members are available but isn't really concerned with any implementation details within these members (which access modifers would fall under).
It's important to remember that interfaces do not actually implement functionality but primarily exist to publically expose the methods and properties that will be available. Interfaces are commonly confused with abstract classes, which actually handle implementation
details as well.
When you actually have a concrete implementation of a class (that isn't an interface) then you can begin to use the normal access modifers as access modifers are implementation details and aren't really necessary on an interface.
Check out the following Stack Overflow discussion which covers this same topic :
May 26, 2013 12:15 PM|nijhawan.saurabh|LINK
The whole aim of an Interface is to expose its method as a contract that the classes implementing it can adhere to.
If you need to access the members in a assembly, you can mark the interface itself as Internal which'd make all it's memebers as internal.
May 26, 2013 11:42 PM|Paul Linton|LINK
This question pops up reasonably often but it is never clear to me what such a feature would provide. An external caller can't make use of the protected member (that's what protected means) so there is no point in advertising the presence of the member
to 'the world'. If you want to ensure that children of a class implement a particular method then you can declare a (protected) virtual function member in the base class. A class knows which hierarchy it is part of so there is no 'discovery' aspect to this.
I can't see how a protected interface member would have any use in a dependency injection scenario but could be wrong.
Do you have a scenario that a protected interface member would provide functionality that you can't get with a virtual function member?