Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the essential reason for the introduction of default keyword in Java8 interface

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What is the essential reason for the introduction of the default keyword in the Java8 interface? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

General implementation of interface

Students who are familiar with the java interface know that once a new method is added to the interface after the interface is implemented by some classes, all classes that implement the interface have to implement the new method (even if the new interface is of no use to some classes).

Define an interface

Public interface IDemo {void func1 ();}

Class CDemo1 implements interface IDemo

Public class CDemo1 implements IDemo {@ Override public void func1 () {}}

Class CDemo2 implements interface IDemo

Public class CDemo2 implements IDemo {@ Override public void func1 () {}}

Add a new method func2 () to IDemo

Public interface IDemo {void func1 (); void func2 ();}

You can see that the method func2 () needs to be enforced in CDemo1 and CDemo2, as shown in the following figure:

Default in the interface

The above example leads to the problem raised earlier. Once a new method is added to the interface, all classes that have implemented the interface have to implement the new method! So what if one of the core interfaces in JDK needs to be changed and some new methods need to be added? This amount of work is even more predictable. Let's take a look at the java.util.Collection interface. Its methods in jdk1.7 include:

The methods included in the Collection interface in JDk 1.8 are as follows:

All the newly added interfaces are modified with default (important changes in the interfaces after jdk1.7 is upgraded to jdk1.8). The source code is as follows:

Default Stream stream () {return StreamSupport.stream (spliterator (), false);} default Stream parallelStream () {return StreamSupport.stream (spliterator (), true);} default Spliterator spliterator () {return Spliterators.spliterator (this, 0);} default boolean removeIf (Predicate)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report