Home  >  Article  >  Java  >  How to define interface in java

How to define interface in java

王林
王林Original
2020-07-11 10:10:094854browse

The way to define an interface in Java is: Interfaces are usually declared with the interface keyword. An interface is an abstract type in the Java language and a collection of abstract methods. Declaration syntax format: [[modifier] interface interface name {declared variable abstract method}].

How to define interface in java

Interface is an abstract type in the JAVA programming language and is a collection of abstract methods. Interfaces are usually declared with the interface keyword.

(Recommended tutorial: java introductory program)

A class inherits the abstract method of the interface by inheriting the interface.

The declaration syntax format of the interface is as follows:

[可见度] interface 接口名称 [extends 其他的接口名] {
        // 声明变量
        // 抽象方法}

Let’s take a look at an example:

/* 文件名 : Animal.java */
interface Animal {
    public void eat();   
    public void travel();
}

(Video tutorial recommendation: java video tutorial)

The interface has the following characteristics:

  • The interface is implicitly abstract. When declaring an interface, you do not need to use the abstract keyword.

  • Every method in the interface is also implicitly abstract, and the abstract keyword is also not required when declaring it.

  • The methods in the interface are all public.

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

Statement:
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