Home  >  Article  >  Java  >  Java basic tutorial interface

Java basic tutorial interface

王林
王林forward
2019-11-29 15:54:022274browse

Java basic tutorial interface

Definition: An interface is a common specification (abstract method inside) of multiple classes, and is a reference data type.

Tips: Basic data types include numerical types (integers and floating point numbers), character types, and Boolean types.

Format:

public interface 接口名称{
	//接口内容
}

Remarks: Interface.java is still interface.class after compilation

Contents contained in the interface:

    java 7(1.常量 2.抽象方法)
    java 8(3.默认方法 4.静态方法)
    java 9(私有方法)

Related video tutorial recommendations: java video tutorial

1. Constants

Constants are similar to the "member variables" of the interface and must be added with public static final is modified by three keywords.

Format:

public static final 数据类型 常量名称 = 数据值;

Note:

1. The three keywords can be omitted;

2. The interface constant must be Copy;

3. For constant names in the interface, use all uppercase letters and separate them with underscores (MY_AGE).

2. Abstract methods

The modifiers of abstract methods are also fixed with two keywords `public abstract` (either one can be omitted Keyword);

Abstract method interface cannot be used directly. There must be an implementation class to implement the interface. The implementation class must override and rewrite (implement) all abstract methods in the interface, otherwise the implementation class It becomes an abstract class.

Format:

public class 实现类名称 iplements 接口名称{
	//方法体
}
public abstract 返回值类型 方法名称(参数列表);

3. Default method

The default method of the interface is used to solve the problem of interface upgrade.

Format:

public default 返回值类型 方法名称(参数列表){
	//方法体
}

Usage: 1. Directly called by the interface implementation class object; 2. Can be overridden and rewritten by the interface implementation class.

4. Static method

public static 返回值类型 方法名称(参数列表){
	//方法体
}

The static method is directly: interface name. Static method name (parameter list).

5. Private methods

If there are multiple default methods (default) in the interface with duplicate code, you can define a common method to solve the problem, but this common method is The default methods used by these methods are private and cannot be used by implementation classes.

1. Ordinary private method (default method default)

private 返回值类型 方法名称(参数列表){
	//方法体
}

2. Static private method (static method static)

private static 返回值类型 方法名称(参数列表){
	//方法体
}

Inherit the parent class and implement multiple interfaces:

1. The interface cannot have static code blocks; it cannot have constructors;

2 , A class can only inherit one parent class but can implement multiple interfaces;

Format:

public class MyInterfaceImpl implements MyInterfaceA,MyInterfaceB{
	//覆盖重写接口所有的抽象方法
	//也可以重写接口内的默认方法
}

3. When there are two or more abstract methods with the same name in the interface, they can be implemented once Yes;

4. When there are two or more default methods with the same name in the interface, this default method needs to be rewritten;

5. A class directly inherits the methods of the parent class and When methods that implement an interface have the same name, the inheritance relationship takes precedence over the interface implementation.

Multiple inheritance between interfaces:

1. Classes and classes have multiple inheritance;

2. Classes and interfaces have multiple implementations;

3. There is multiple inheritance between interfaces.

Recommended related article tutorials: Introduction to java programming

The above is the detailed content of Java basic tutorial interface. For more information, please follow other related articles on the PHP Chinese website!

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