locked
Interface Members only public RRS feed

  • Question

  • User436073879 posted

    Why interface members are only public?? Why not protected or any other access specifiers we can use in interface members??

    Wednesday, May 22, 2013 4:45 PM

Answers

  • User281315223 posted

    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 : 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 22, 2013 4:51 PM
  • User1320101480 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 26, 2013 12:15 PM
  • User-1910946339 posted

    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?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 26, 2013 11:42 PM

All replies

  • User281315223 posted

    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 : 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 22, 2013 4:51 PM
  • User1320101480 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 26, 2013 12:15 PM
  • User-1910946339 posted

    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?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 26, 2013 11:42 PM