Home > Java > javaTutorial > body text

How to define and implement java interface

WBOY
Release: 2023-05-01 16:16:14
forward
2242 people have browsed it

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  接口名{
抽象方法一;
抽象方法二;
抽象方法三;
..........}
Copy after login

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");
    }
}
Copy after login

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!

Related labels:
source:yisu.com
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