Home > Java > javaTutorial > body text

How to use interface default methods in Java 8

PHPz
Release: 2023-04-22 15:37:16
forward
1278 people have browsed it

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

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

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
在主方法中,按照多态的方式创建对象并使用
Copy after login

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!

Related labels:
source:yisu.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!