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.
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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!