Home  >  Article  >  Java  >  What can the modifiers of java interface be?

What can the modifiers of java interface be?

青灯夜游
青灯夜游Original
2020-10-21 16:21:4511925browse

The modifier of the Java interface can be "public abstract". The interface is used to describe all the services provided by the system, so the member constants and methods in the interface must be public types; 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.

What can the modifiers of java interface be?

The modifier of the Java interface defaults to public abstract because the interface must be implemented.

Interface is very important. In order to explain the situation, here is a little more verbose:

(1) The interface is used to describe all the services provided by the system, so the member constants and methods in the interface must be Public types 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. Method;

(3) 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 variables;

(4) The variables in the interface are common to all implementation classes. Since they are common, they must be constant, because changing things cannot be counted as common. 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 embody the higher abstraction level of the interface? How to embody 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 methods of the interface are public abstract by default;

You cannot define variables in the interface, that is, you can only define constants (with final modification, it will become constant). Therefore, the properties of the interface are public static final constants by default and must be assigned an initial value. **

The above is the detailed content of What can the modifiers of java interface be?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn