search

Home  >  Q&A  >  body text

java - 为什么接口的方法的访问修饰符只能是public不能是protected?

为什么抽象类的方法的访问修饰符可以是public和protected,而接口的方法的访问修饰符只能是public?

或者说,

接口的方法的访问修饰符为什么不能是protected?

PHP中文网PHP中文网2887 days ago1069

reply all(5)I'll reply

  • PHPz

    PHPz2017-04-17 17:34:01

    Personal guess:

    • Language designers may feel that this increases the complexity of the access model of the interface.

    • It will be very difficult for the class responsible for implementing the interface. If the implementation and interface are not in the same package, should you implement a protected method?

    I will use two examples to illustrate below. The first one makes you feel that protected interface method does not make sense. The other one makes you think, it doesn't make sense at all.

    For example, in sample.interface这个包下面有一个ISomething.

    package sample.interface;
    interface ISomething {
      protected foo1() { ... }
    }

    The following case is weird.

    package a.place.of.nowhere;
    // some code tries to use ISomething
    ISomething someting = initializeSomething();
    
    something.foo1();   // 抱歉,foo1是被保护的,和你不是一个package的,你滚吧。
    // ME: shit... if I cannot use anything of this interface, why the heck you expose this interface to me...

    Even less make sense:

    package a.place.of.nowhere;
    // some code tries to implements ISomething
    class SomethingImpl implements ISomething {
       // 抱歉,尽管你想implement foo1这个接口方法,但是由于你在a.place.of.nowhere这个package,foo1()对你是不可见的。走人吧。
       protected void foo1() {...}

    In this way, class implement and interface will be under one package. What the hell is this? I may have to implement multiple interfaces, but I can’t do them separately. . .

    reply
    0
  • 阿神

    阿神2017-04-17 17:34:01

    Interfaces play more of a role in system architecture design methods, and are mainly used to define communication contracts between modules.
    Abstract classes play a role in code implementation and are mainly used to achieve code reuse.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:34:01

    Abstract and interface are used in different situations. The former overrides on the premise of inheriting parameters, but the target is its direct or indirect parent class, and protected permissions are allowed. Interface is usually used for callbacks in different classes, such as listener. If it has protected permissions, even if you get the listener instance, you cannot get its methods, so the callback itself is meaningless.
    A little personal opinion, please point out if there are any mistakes

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:34:01

    What is the interface? The interface is written as interface, which is used for interaction with the outside world. If you write it as protected, how can you interact with the outside world?

    Interfaces are used to stipulate that an object passed in must implement certain methods. If the implemented methods cannot be called, then what is the meaning of this stipulation? It can't be called anyway, since the object is the same no matter what you wear.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:34:01

    The interface is for implementation. It is an outsider, not an inheritance relationship, not an immediate relative. If currency money=1^n, you cannot give him a share of money, and he cannot enjoy it.
    Protect is visible to the inherited subclass. If it is your own person, just inherit all the money to him.
    If the interface is set to protect, then she is either a mistress or a mistress, which is illegal.

    reply
    0
  • Cancelreply