Home  >  Article  >  Java  >  How to use interface default methods in Java 8

How to use interface default methods in Java 8

PHPz
PHPzforward
2023-04-22 15:37:161245browse

1. The format of the default method in the interface:

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

Notes on the default method in the interface:

2. The default method It is not an abstract method, so it is not rewritten. But it can be rewritten, and keywords can be deleted during rewriting.

public can be omitted, but default cannot be omitted.

Example

1:定义一个接口MyInterface,里面有两个抽象方法:
void show1();
void show2();
2:定义接口的两个实现类:
MyInterface ImplOne
MyInterface ImplTwo
3:定义测试类:
MyInterfaceDemo
在主方法中,按照多态的方式创建对象并使用

The above is the detailed content of How to use interface default methods in Java 8. For more information, please follow other related articles on the PHP Chinese website!

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