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 default method for Java8

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of what the default method of Java8 is, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this Java8 default method. Let's take a look at it.

What is the default method

The default method is the method decorated with default in the interface, which contains the method content.

For example, the following:

Public interface InterfaceDemo {/ / ordinary method, which only defines, does not implement void oldFun (); / / default method, which defines and implements default void newFun () {System.out.println ("newFun");}} Why should the default method be provided?

For backward compatibility (which is one of the reasons why Java becomes bloated).

Because when upgrading the system, it is inevitable that some new functions need to be added, so if a method is added to the interface class, the implementation class must modify the implementation synchronously.

In this way, the workload is still very large, and it is easy to make mistakes.

So Java8 began to introduce the default method of the interface, which makes the interface upgrade smoother.

For example, the following code: InterfaceDemo is the above interface

Public class UserDemo implements InterfaceDemo {@ Override public void oldFun () {System.out.println ("oldFun");} public static void main (String [] args) {UserDemo demo = new UserDemo () After InterfaceDemo upgrade, the newFun method is added * but since newFun is the default method, implementation content is provided * so the subclass UserDemo here can directly use * / demo.newFun ();}}

We can see that UserDemo does not implement the new method newFun (), but it can also be compiled and run and call newFun () directly.

This is the benefit of the default method: painless upgrade for implementing classes

And if you don't?

If not, the system has two options when the interface class is upgraded

Implement class upgrade:

The implementation class honestly modifies and implements synchronously according to the method after the interface upgrade, but the workload is heavy.

The implementation class does not upgrade:

It is OK not to upgrade the implementation class, as long as a new version of the interface class is not introduced, then the system can still run at this time, which is no problem. But who can guarantee that the system will not be updated for the rest of his life? If the interface class library is upgraded to a new version when the system is updated, the compilation will still fail.

Who is it mainly aimed at?

The default method of the interface is mainly for the class library designer

Is there any difference between an interface that implements the default method and an abstract class

There are not as many differences as before, but there are still some:

Abstract class single inheritance, interface class multi-implementation

The attribute definition in the abstract class does not need to be initialized, but the property definition of the interface class needs to be initialized (the default modifier is public static final).

Can it be said that Java now implements multiple inheritance?

You could say that.

But now we are faced with a new problem, that is, the ambiguity problem caused by multiple inheritance, which is similar to the fatal square (also known as the diamond problem) introduced earlier.

As shown in the UML diagram below

For example, you can't know which interface's fun method A will call.

So the compiler reports an error:

Com.jalon.java8.defaultmethod.An inherits unrelated defaults for fun () from types com.jalon.java8.defaultmethod.B and com.jalon.java8.defaultmethod.C

Solution:

Override the fun method first

Then display the fun method that declares which interface to call

The code is as follows:

Public class An implements new C {@ Override public void fun () {/ / shows the default method of calling B: B.super.fun ();} public static void main (String [] args) {AA = new A (); / / the fun a.fun () of B is printed here;}} interface D {default void fun () {System.out.println ("D") }} interface B extends D {@ Override default void fun () {System.out.println ("B");}} interface C extends D {@ Override default void fun () {System.out.println ("C");}} this is the end of the article on "what is the default method for Java8". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the default method of Java8". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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