Default methods instance resolution

This is how the Oracle official website introduces the default method. Using the default method can add new functions to the interface and maintain compatibility with the old version code, that is, the original implementation class does not need to be modified passively. Therefore, the default method location is in the interface; The default method has an implementation and does not force concrete classes to appear. To make full use of lambda, Java 8 needs to enhance a large number of class libraries, but if you want to achieve compatibility, you can only use the default method.

Default method

Compared with ordinary interface methods, the default method is implemented by adding the default keyword at the front, followed by braces after the parameter list, and then there is no semicolon after it.

If you look at the interface of the JDK source code, you will find that there is more default keyword.

A brief introduction.

Default enables us to add new functionality to the interface without interrupting the class that implements the interface. Let's take a look at the following example.

The above code shows the method saysomething() of the class MyClass that implements interfacea. Now let's add a new method called sayhi () to interfacea. By doing so, we introduce a problem to the class MyClass because it will not compile until we provide an implementation of the method sayhi().

Then default is useful. By adding the keyword default before the method's access modifier, we don't have to provide an implementation for the method sayhi () in class MyClass.

In the strictest sense, defaults are a step back because they allow you to "pollute" your interface with code. But they provide the most elegant and practical way to allow backward compatibility. It makes it easier for the JDK to update all collections classes and retrofit existing lambda code for you. (in fact, this is the initial reason for default.)

Note that we must provide implementations of all default methods. Therefore, the default method gives us the flexibility to implement the method in the interface. If the concrete class does not provide an implementation of the method, the implementation will be used as the default.

Multi interface conflict

Since classes in Java can implement multiple interfaces, there may be a case where two or more interfaces have a default method with the same name, resulting in a conflict because Java does not know what method to use at a time. This will cause compilation errors: class MyClass inherits default sayhi () from types interfacea and interfaceb. Let's take a look at the following example.

In order to solve this situation, we must provide the implementation of sayhi () method in class MyClass, so we override the two methods in interfacea and interfaceb.

If we want to specifically call a sayhi () method in interfacea or interfaceb, we can also do this:

summary

The above is all about the default methods instance parsing in this article. I hope it can be helpful to you.

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
分享
二维码
< <上一篇
下一篇>>