The modifiers of java interface can be abstract and final. The interface can be understood as a special class. The difference is that the members of the interface have no execution body and are composed of global constants and public abstract methods; the methods of the interface are public abstract by default; and variables cannot be defined in the interface. Constants can be defined (with final modification, they will become constants), so the properties of the interface are public static final constants by default and must be assigned an initial value.
The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.
Abstract class is a template abstracted from multiple classes. If this abstraction is carried out more thoroughly, a more special "abstract class" - interface (Interface) can be extracted. Interface is one of the most important concepts in Java. It can be understood as a special class. The difference is that the members of the interface have no execution body and are composed of global constants and public abstract methods.
Define interface
The definition of Java interface is basically the same as that of class, but the keyword used in interface definition is interface, and the syntax of interface definition is The format is as follows:
[public] interface interface_name [extends interface1_name[, interface2_name,…]] { // 接口体,其中可以包含定义常量和声明方法 [public] [static] [final] type constant_name = value; // 定义常量 [public] [abstract] returnType method_name(parameter_list); // 声明方法 }
The description of the above syntax is as follows:
public represents the modifier of the interface. When there is no modifier, the default modifier is used. This The access rights of the interface are limited to the package to which it belongs;
interface_name represents the name of the interface. The interface name should adopt the same naming rules as the class name, that is, from a grammatical perspective, the interface name only needs to be a legal identifier. To comply with Java readability specifications, an interface name should be a concatenation of meaningful words, with the first letter of each word capitalized, without any separators between words.
extends represents the inheritance relationship of the interface;
interface1_name represents the name of the interface to be inherited;
constant_name represents the variable name, usually static and final;
returnType represents the return value type of the method;
parameter_list represents the parameter list , methods in interfaces have no method body.
Note: An interface can have multiple direct parent interfaces, but interfaces can only inherit interfaces, not classes.
Modifiers of java interface
The interface is very important. In order to explain the situation, here is a little more verbose:
(1) The interface is used to describe all services provided by the system to the outside world, so the member constants and methods in the interface must be of public type to ensure that external users can access them;
(2) The interface only describes what the system can do, but does not specify how to do it, so the methods in the interface are abstract methods;
(3) The interface does not involve details related to any specific instance, so the interface There is no constructor, it cannot be instantiated, there are no instance variables, only static variables;
(4) The variables in the interface are common to all implementation classes. Since they are common, they must be unchanged. , because things that change cannot be counted as shared. Therefore, the variable is an immutable (final) type, which is a constant.
(5) Variables cannot be defined in the interface? If the interface can define variables, but the methods in the interface are abstract, 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 this situation. If there is a static variable a with public access in interface A. According to Java's semantics, we can access variable a without using the object that implements the interface. We can change the value of variable a in the interface through A.a = xxx;. Just as this can be done 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? Then what is the use of the abstraction of the interface? Therefore, variables cannot appear in the interface. If there are variables, it conflicts with the idea of unified abstraction provided by the interface. Therefore, the attributes in the interface must be constants, which can only be read and cannot be changed. Only in this way can a unified attribute be provided for the object that implements the interface.
In layman's terms, if you think something needs to be changed, put it in your own implementation and not in an interface. An interface is just a higher-level abstraction of the attributes and behaviors of a type of thing. Closed for modification and open for extensions (different implementations), the interface is a manifestation of the open-closed principle.
So:
The default method of the interface is public abstract
;
You cannot define variables in the interface, only constants (plus final modification) will become a constant). Therefore, the properties of the interface are public static final
constants by default, and must be assigned an initial value.
Note: final and abstract cannot appear at the same time.
For more programming-related knowledge, please visit: Programming Teaching! !
The above is the detailed content of What can be the modifiers of java interface. For more information, please follow other related articles on the PHP Chinese website!