Why can't I have protected interface members?

后端 未结 13 1856
走了就别回头了
走了就别回头了 2020-11-29 19:58

What is the argument against declaring protected-access members on interfaces? This, for example, is invalid:

public interface IOrange
{
    public OrangePee         


        
相关标签:
13条回答
  • 2020-11-29 20:20

    Can't see why would one want this. If you want derived class to provide an implementation of a particular method, go for abstract base classes. Interfaces are just that - interfaces. A public contract, nothing else. Think of interface as of specification which describes how should the implementation look to the outside world. A specification for a two-pin plug does not state (at least I assume that) what it's internal structure should be like. It just must be interface-compatible with a plug socket.
    (source: made-in-china.com)

    0 讨论(0)
  • 2020-11-29 20:23

    Any class which implements a .net interface will have to include implementations of all of the interface members. Further, any class can expose to a derived class whatever members it wishes. Requiring that an implementation of an interface must include a member which will only be usable from derived classes would serve no useful purpose, unless either (1) such a member could be visible to something outside the interface, or (2) interface implementations could use members which they did not themselves define. If interfaces were allowed to include nested classes (which could access the interfaces' protected members), then protected interface members would make sense. Indeed, they could be very useful if a class nested within an interface could define extension methods for that interface. Unfortunately, no such facility exists.

    BTW, even without being able to nest classes in interfaces, it would still be useful to apply an internal access modifier to interface members, with the effect that only the assembly where the interface is defined would be able to define any implementations for it.

    0 讨论(0)
  • 2020-11-29 20:24

    An Interface contains only public members. Protected means whatever you're declaring is only available to the class and derived class instances.

    0 讨论(0)
  • 2020-11-29 20:27

    in contrast to abstract base classes, protected interfaces would allow "multiple inheritance" (of abstract classes), which i would have found useful once or twice...

    0 讨论(0)
  • 2020-11-29 20:30

    Because it makes no sense. An interface is a publicly exposed contract. I am an IThing, therefore I will perform IThing methods if asked. You can't ask an IThing to confirm it performs methods it can't tell you about.

    0 讨论(0)
  • 2020-11-29 20:31

    I think everyone hammered the point of an interface having only public members, no implementation details. What you are looking for is an abstract class.

    public interface IOrange
    {
        OrangePeel Peel { get; }
    }
    
    public abstract class OrangeBase : IOrange
    {
        protected OrangeBase() {}
        protected abstract OrangePips Seeds { get; }
        public abstract OrangePeel Peel { get; }
    }
    
    public class NavelOrange : OrangeBase
    {
        public override OrangePeel Peel { get { return new OrangePeel(); } }
        protected override OrangePips Seeds { get { return null; } }
    }
    
    public class ValenciaOrange : OrangeBase
    {
        public override OrangePeel Peel { get { return new OrangePeel(); } }
        protected override OrangePips Seeds { get { return new OrangePips(6); } }
    }
    

    Edit: It is fair to argue that if we have a PlasticOrange that derives from a class Ornament, it can only implement IOrange and not the Seeds protected method. That is fine. An interface by definition is a contract between a caller and an object, not between a class and its subclasses. The abstract class is as close as we come to this concept. And that is fine. What you are essentially proposing is another construct in the language through which we can switch subclasses from one base class to another without breaking the build. To me, this doesn't make sense.

    If you are creating a subclass of a class, the subclass is a specialization of the base class. It should be fully aware of any protected members of the base class. But if you suddenly want to switch the base class out, it makes no sense that the subclass should work with any other IOrange.

    I suppose you have a fair question, but it seems like a corner case and I don't see any benefit from it to be honest.

    0 讨论(0)
提交回复
热议问题