Java The modifier of an interface can be （）
A private B protected C final D abstract
（1） The interface is used to describe all services provided by the system , Therefore, member constants and methods in an interface must be public (public) Of type , Ensure that external users have access to them ;
（2） The interface simply describes what the system can do , But I don't know how to do it , So the methods in the interface are abstract (abstract) method ;
（3） The interface does not involve any specific instance related details , So the interface has no constructor , Cannot be instantiated , No instance variable , Static only （static） variable ;
（4） The variables in the interface are common to all implementation classes , Since we share , It must be the same thing , Because changing things can't be counted as co ownership . So variables are immutable (final) type , That's constant .
（5） Variables cannot be defined in an interface ? If the interface can define variables , But the methods in the interface are all abstract , Properties cannot be modified by behavior in an interface . Some people will say , It doesn't matter. , Through
Implements the behavior of objects in an interface to modify properties in the interface . Of course, there's no problem , But consider the situation . If the interface A One of them public Static variables for access rights a. according to Java
Semantics of , We can access variables without implementing objects of the interface a, adopt A.a = xxx; You can change the variables in the interface a The value of . As can be done in an abstract class , Then implement the interface
A All objects of will automatically own the changed a The value of , That is to say, a place has changed a, In all of these objects a
The value of . What's the difference between this and abstract classes , How to embody the higher abstraction level of interface , How to embody the unified protocol provided by the interface , What else should we do with the abstraction of interface ? So in the interface
Variable cannot appear , If there are variables , The idea of a unified abstraction provided by an interface is contradictory . So the attributes in the interface must be constants , Can only read but not change , In this way, we can provide a unified system for the objects that implement the interface Properties of one .
Popular speaking , What you think is going to change , In your own realization , It can't be put in the interface , Interface is only a higher level abstraction of the attributes and behaviors of a kind of things . Close modification , For extension （ Different implementations
implements） to open up , Interface is an embodiment of the principle of opening and closing .
The default method of the interface is public abstract;
No variables can be defined in the interface, i.e. only constants can be defined ( add final Modification becomes constant ). So the default property of the interface is public static final constant , And initial value must be assigned .
be careful ：final and abstract Can't appear at the same time .