Home > Java > JavaBase > How to define an interface in java

How to define an interface in java

王林
Release: 2019-12-04 16:56:52
Original
8004 people have browsed it

How to define an interface in java

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

Description:

Modifier: Optional, used to specify the access permission 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.

java video tutorial recommendation: java learning

How to define an interface in java

##public abstract void eat(); //Interface Only abstract methods can be defined in

void eat(); //The methods defined in the interface do not declare modifiers, and the default is public abstract

public static final int NUM = 10; //Define a constant in the interface

int NUM = 10; //Constant

New after JDK1.8 Properties can be implemented by all classes.

//Multiple inheritance is possible between interfaces (note: classes can only inherit singly)

//A specific class must implement all methods of the interface to implement the interface

The concept of interface

1. An interface is a set of behavioral specifications and definitions without implementation (JDK1.8 default method)

2. Using interfaces can make our programs More conducive to change

3. Interface is one of the essence of ideas in object-oriented programming system

4. Object-oriented design rules: based on interface programming

Interface Usage rules

(1) To define an interface, use the interface keyword;

(2) In an interface, only constants and abstract methods can be defined, which can be done after JDK1.8 Define the default implementation method;

(3) The interface can inherit multiple interfaces, extends xxx, xxx;

(4) A concrete class implements the interface using the implements keyword;

(5) A class can implement multiple interfaces;

(6) The abstract implementation interface does not need to implement the methods of the interface;

(7) The methods defined in the interface do not declare access Modifier, the default is public;

(8) The interface cannot have a constructor method;

(9) The interface cannot be instantiated.

Recommended related articles and tutorials:

Getting started with java

The above is the detailed content of How to define an 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