Java 8 default method

summary

Java 8 adds default methods for interfaces. Use the default keyword.

The default method is that the interface can have implementation methods, and no implementation class is required to implement its methods. Relative to jdk1 For the interface before 8, the method that can be implemented in the interface is added.

It can be said that part of the reason for implementing methods in the interface is to serve lambda expressions, because lambda expressions can only be handed over to the interface.

classification

grammar

Multiple default methods

Because there are default methods in the interface, a class can implement multiple interfaces. It is inevitable to encounter methods with the same name. When a class implements multiple interfaces and there are default methods with the same name in multiple interfaces, subclasses need to be re implemented to avoid ambiguity

Static default method

Java 8 interfaces can support static methods and provide implementations.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>