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