Home Java javaTutorial What can the modifiers of java interface be?

What can the modifiers of java interface be?

Oct 21, 2020 pm 04:21 PM
java interface

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

See all articles