Why cannot private and protected be used for classes and interfaces? Private, protected, default, and privateprotected cannot be used for interface methods.
For java programmers, the differences and usage of the access permission modifiers public, protected, default, and private in java are certainly not a problem. Here, I will not be so embarrassed. Anyway, I will try again when I search for them. Recently, when organizing training materials for java core, I came up with several questions about permission modifiers:
The reason is not difficult. The main reason is that although java has been implemented for a short period of time, I have never seriously thought about this problem. Write down your ideas, think more, and do not become a framework Porter.
First question:
Private:
It is easy to understand that if classes and interfaces are defined as private, no other classes can be accessed. Writing such classes is meaningless.
Protected:
This problem is explained by the reverse identification method. If class B and class A are not in the same package, and A is A protected class, the premise that B can access Class A is that B is A subclass of Class, the premise that B can become A subclass of A (or B can inherit A) Is that B can access. These two conditions are mutually preconditions and cannot be achieved.
Second question:
Private:
The interface must be implemented by other classes. If the method is defined as private, no other class can be accessed. This method requires implementation and is invisible to any class, which cannot be implemented.
Protected:
(1) assume that the public interface I has a protected method M, then the public class C located in other packages can implement this interface (the method M is still protected ). Then the same class D calling method M of C can only be accessed as follows:
C c = new C();c.M();
Access fails as follows:
I c = new C();c.M();
In this way, the importance of using interfaces is lost: the idea of interface-oriented programming cannot be reflected by providing unified interfaces.
(2) Assume that interface I is default, then the method M is protected theoretically, and M can also be default or public. As to why do not use protected, it may be for the purpose of simplifying the complexity of modifier (if you use it, you need to distinguish that the interface cannot be used when it is public, and the default interface can be used ), for the default value, see (3 ).
Default:
(3) Assuming that public interface I has a default method M, the interface I cannot be implemented normally in other packages because method M is invisible to it.
(4) assume that the default Interface I has a default method M, then the method M is default theoretically, and M can also be proteced and public. It may be because of the same reason as (2) that protected cannot be used, and default cannot be used.
Therefore, based on the above four points, the interface method can only use public. Since only public can be used, the java compiler will automatically convert you to public when you forget to write public before M.
Finally, for my personal understanding, I have not found any official explanation. If there are any mistakes, I would like to ask for further proof by myself when I read this text. You are welcome to discuss it :)