Home  >  Article  >  Java  >  How to define and implement java interface

How to define and implement java interface

WBOY
WBOYforward
2023-05-01 16:16:142184browse

1. Concept

Use interface to define the interface. Classes similar to interface definitions are divided into interface declarations and interface questions, where interfaces consist of constant definitions and method definitions.

A class needs to use the keyword implements in the class declaration to declare that the class implements one or more interfaces.

2. Definition format

public interface  接口名{
抽象方法一;
抽象方法二;
抽象方法三;
..........}

3.Interface implementation instance

package com.dao.util;
 
public class Util {
    public interface Worker {
        void work();
    }
}
  
package com.dao.util;
 
import com.dao.util.Util.Worker;
 
public class Demo implements Worker {
    public void work() {
        System.out.println("Is working");
    }
}

The above is the detailed content of How to define and implement java interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete