Home > Java > javaTutorial > How to use implements in java

How to use implements in java

下次还敢
Release: 2024-05-09 04:48:19
Original
511 people have browsed it

implements keyword is used in Java to declare that a class or interface implements other interfaces and provides methods and functions that implement the methods and functions declared in the interface. Specific usage includes: 1. Class or interface keyword followed by implements keyword and interface name; 2. Class or interface implements unimplemented methods in the interface and inherits constants, fields and default methods in the interface; 3. Classes can be implemented through implements Multiple interfaces.

How to use implements in java

Implements usage in Java

implements keyword is used in Java to indicate a class or interface implementation Another interface. It indicates that the class or interface will provide methods and functions that implement the methods and functions declared in the interface.

Usage

class or interface keyword followed by the implements keyword and a or multiple interface names. Interfaces are separated by commas as follows:

<code class="java">public class ClassName implements Interface1, Interface2 {
    // 类代码
}

public interface InterfaceName extends ParentInterface {
    // 接口代码
}</code>
Copy after login

Function

  • ##Implementation methods:Implements all unimplemented statements declared in the interface Methods.
  • Inherit interface: A class or interface can inherit the constants, fields and default methods in the interface.
  • Multiple inheritance: A class can implement multiple interfaces through implements.

Example

<code class="java">// 定义一个接口
interface Drawable {
    void draw();
}

// 实现该接口的类
class Rectangle implements Drawable {
    @Override
    public void draw() {
        // 绘制矩形
    }
}

// 定义一个继承接口的接口
interface Shape extends Drawable {
    void rotate();
}

// 实现该接口的类
class Circle implements Shape {
    @Override
    public void draw() {
        // 绘制圆形
    }

    @Override
    public void rotate() {
        // 旋转圆形
    }
}</code>
Copy after login
In this example, the

Rectangle class implements the Drawable interface, and ## The #Circle class implements the Shape interface, and it also inherits the methods in the Drawable interface.

The above is the detailed content of How to use implements in java. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template