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

What can be the modifiers of java interface?

DDD
Release: 2023-08-30 14:05:02
Original
2811 people have browsed it

The modifiers of the java interface can be public, default, abstract and strictfp. Detailed introduction: 1. public, indicating that the interface is public and can be accessed by any class; 2. default, used to define the default method in the interface, which can be called directly in the interface or overridden in the implementation class; 3. , abstract, used to define abstract interfaces, which need to be implemented concretely in the implementation class; 4. strictfp, used to define the precision of floating point operations in the interface, etc.

What can be the modifiers of java interface?

# Operating system for this tutorial: Windows 10 system, Dell G3 computer.

There are four types of interface modifiers in Java, namely public, default, abstract and strictfp. These four modifiers will be introduced in detail below.

1. Public modifier:

The public modifier is the most commonly used interface modifier, which indicates that the interface is public and can be accessed by any class. When an interface is declared public, other classes can use the methods and constants defined in the interface by implementing the interface. In Java, a class can only inherit from one parent class, but can implement multiple interfaces. Therefore, the public modifier allows the interface to be implemented and used in multiple classes.

2. default modifier:

The default modifier is a new interface modifier in Java 8. It is used to define default methods in interfaces. A default method refers to a method that provides a default implementation in the interface. This method can be used directly in the implementation class of the interface without reimplementing it in the implementation class. Default methods defined using the default modifier can be called directly in the interface or overridden in the implementation class.

3. abstract modifier:

The abstract modifier is used to define abstract interfaces. Abstract interface refers to an interface that contains abstract methods. Abstract method refers to a method without specific implementation, only the declaration of the method. An abstract interface cannot be instantiated and can only be used through a class that implements the interface. Abstract methods in abstract interfaces need to be implemented concretely in the implementation class.

4. strictfp modifier:

The strictfp modifier is used to define the precision of floating point operations in the interface. In Java, the results of floating point operations may vary depending on the platform. Use the strictfp modifier to ensure that floating point operations produce consistent results on different platforms. The strictfp modifier can be used on interface methods and constants.

To sum up, there are four modifiers of interfaces in Java: public, default, abstract and strictfp. The public modifier indicates that the interface is public and can be accessed by any class; the default modifier is used to define the default method in the interface; the abstract modifier is used to define an abstract interface; the strictfp modifier is used to define the precision of floating point number operations in the interface . According to specific needs, you can choose appropriate modifiers to modify the interface.

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template