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

How to define interface in java

(*-*)浩
Release: 2019-12-03 13:53:09
Original
4584 people have browsed it

How to define interface in java

Interface (English: Interface), in the JAVA programming language, is an abstract type and a collection of abstract methods. The interface is usually declared with interface. A class inherits the abstract methods of the interface by inheriting the interface.​ ​ ​ ​ ​ (Recommended learning: java course)

Interfaces are not classes. The way to write interfaces is very similar to classes, but they belong to different concepts. Classes describe the properties and methods of an object. The interface contains the methods that the class implements.

Use interface to define an interface. Interface definitions are similar to similar definitions and are also divided into interface declarations and interface bodies. The interface body consists of two parts: constant definition and method definition. The basic format for defining an interface is as follows:

[修饰符] interface 接口名 [extends 父接口名列表]{

    [public] [static] [final] 常量;  //全局常量
    [public] [abstract] 方法;  //抽象方法
}
Copy after login

Modifier: Optional, used to specify the access rights of the interface, the optional value is public. If omitted the default access permissions are used.

Interface name: A required parameter, used to specify the name of the interface. The interface name must be a legal Java identifier. Generally, capital letters are required.

extends Parent interface name list: Optional parameter, used to specify which parent interface the interface to be defined inherits from. When using the extends keyword, the parent interface name is a required parameter.

Methods: The methods in the interface are only defined but not implemented.

Interfaces have the following characteristics:

Interfaces are 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.

Example

Animal.java file code:

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

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!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template