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 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. . .
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.
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
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.
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.
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
.The following case is weird.
Even less make sense:
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. . .
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.
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
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.
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.