Detailed explanation of Java interface modifiers - modifiers that can modify interfaces

Detailed explanation of Java interface modifiers



The modifiers that can modify the interface are ( CD )

A private   B protected   C  final    D abstract

The interface is to provide a unified "protocol", and the attributes in the interface are also members of the "protocol". They are public, static, final constants. Equivalent to global constants. An abstract class is not a "complete" class, which is equivalent to an intermediate layer between the interface and the concrete class. It satisfies both the abstraction of the interface and the concrete implementation.

 

Interface is an important magic weapon to build loosely coupled software system.

 

A. The interface is used to describe all the services provided by the system externally, so the member constants and methods in the interface must be of public type to ensure that external users can access them;

 

B. The interface only describes what the system can do, but does not specify how to do it, so the methods in the interface

Both are abstract methods;

 

C. The interface does not involve details related to any specific instance, so the interface has no construction method, cannot be instantiated, has no instance variables, only static (static) variables.

 

D. The variables in the interface are shared by all implementation classes. Since they are shared, they must be constant, because things that change cannot be considered shared. So the variable is an immutable (final) type, that is, a constant.

 

E. Why can’t variables be defined in the interface? If the interface can define variables, but the methods in the interface are all abstract, and the properties cannot be modified through behavior in the interface. Some people will say, it doesn't matter, you can modify the properties in the interface through the behavior of the object that implements the interface. This is of course no problem, but consider a situation like this. If there is a static variable a with public access in interface A. According to the semantics of Java, we can not access the variable a through the object that implements the interface, and we can change the value of the variable a in the interface through Aa = xxx;. Just as it is possible to do this in an abstract class, all objects that implement interface A will automatically have the changed value of a, that is to say, if a is changed in one place, the value of a in all these objects will also follow. changed. What is the difference between this and an abstract class? How to reflect the higher abstraction level of the interface, how to reflect the unified protocol provided by the interface, and what is the abstraction of the interface? Therefore, variables cannot appear in the interface. If there are variables, it is in conflict with the idea of ​​a unified abstraction provided by the interface. Therefore, the properties in the interface must be constants, which can only be read and cannot be changed, so as to provide a unified property for the objects that implement the interface.

 

In layman's terms, what you think is to be changed should be placed in your own implementation, not in the interface. The interface is just a higher-level abstraction of the attributes and behaviors of a class of things. Closed for modification, open for extension (different implementations), the interface is a manifestation of the principle of opening and closing.

 

so:

The default method of the interface is public abstract

Variables cannot be defined in the interface, that is, the defined variables must be preceded by final modification to make them constants (variables without final modification, and final modification will become constants). Therefore, the properties of the interface are public static final constants by default, and must be assigned initial values.

Guess you like

Origin blog.csdn.net/u014304688/article/details/71211295