Home > Java > javaTutorial > How to write java interface

How to write java interface

小老鼠
Release: 2024-01-03 15:14:52
Original
989 people have browsed it

In Java, an interface is a structure that defines a collection of abstract methods. It provides a way to separate the signature of an abstract method from its concrete implementation. Interfaces are declared using the keyword interface. Features and precautions in interfaces: 1. The methods in the interface default to public, and abstract methods do not need to be declared with the abstract keyword; 2. The fields in the interface default to public, static, and final; 3. The interface cannot contain instance fields. But it can contain constants; 4. A class can implement multiple interfaces, but can only inherit one class, etc.

How to write java interface

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

In Java, an interface is a structure that defines a collection of abstract methods. It provides a way to separate the signature of an abstract method from its concrete implementation. Interfaces are declared with the keyword interface. The following is the basic syntax for writing Java interfaces:

public interface MyInterface {
    // 声明一个抽象方法(接口中的方法默认为抽象方法)
    void myMethod();
    // 可以包含常量(默认为public static final)
    int MAX_VALUE = 100;
    // 可以包含默认方法(使用default关键字)
    default void defaultMethod() {
        System.out.println("Default method implementation");
    }
    // 可以包含静态方法(使用static关键字)
    static void staticMethod() {
        System.out.println("Static method implementation");
    }
}
Copy after login

In the above example, MyInterface is a simple interface that contains an abstract method myMethod(), a constant MAX_VALUE, a default method defaultMethod() and a static method staticMethod().

Features and precautions in interfaces:

Methods in interfaces are public by default, and abstract methods do not need to be declared with the abstract keyword.

Fields in the interface default to public, static, and final (public static final).

Interfaces cannot contain instance fields, but they can contain constants.

A class can implement multiple interfaces, but can only inherit one class.

An interface can inherit multiple interfaces.

Default methods and static methods in interfaces provide a mechanism for backward compatibility as the interface evolves.

The following is an example of a class implementing an interface:

public class MyClass implements MyInterface {
    @Override
    public void myMethod() {
        System.out.println("Implementation of myMethod");
    }
    // MyClass 可以选择重写默认方法 defaultMethod()
    public static void main(String[] args) {
        MyClass myObj = new MyClass();
        myObj.myMethod();
        myObj.defaultMethod();
        MyInterface.staticMethod();
    }
}
Copy after login

In this example, the MyClass class implements the MyInterface interface and provides a specific implementation of the myMethod method. In the main method, it demonstrates how to create an instance of MyClass and call methods in the interface.

The above is the detailed content of How to write 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