What is the default method?
After Java 8 is released, new methods can be added to the interface, but the interface can still be compatible with its implementation class. This is very important because the class library you develop may be widely used by multiple developers. Before Java 8, after an interface was released in the class library, if a new method was added to the interface, those applications that implement this interface would be at risk of crashing using the new version of the interface.
With Java 8, isn’t there no such danger? The answer is no.
Adding a default method to an interface may make some implementation classes unavailable.
First, let's look at the details of the default method.
In Java 8, methods in interfaces can be implemented (the static methods in Java 8 can also be implemented in interfaces, but this is another topic). The method implemented in the interface is called the default method, which is identified by the keyword default as a modifier. When a class implements an interface, it can implement methods that have been implemented in the interface, but this is not necessary. This class will inherit the default method. This is why when the interface changes, the implementation class does not need to be changed.
What about the time to inherit more?
Things become complicated when a class implements more than one (for example, two) interfaces and these interfaces have the same default method. Which default method does the class inherit? None of them are! In this case, the class must implement the default method (only) by itself (directly or inheriting the higher-level classes on the tree).
The same is true when one interface implements the default method and the other interface declares the default method as abstract. Java 8 tries to avoid unclear things and keep it rigorous. If a method is declared in multiple interfaces, then any default implementation will not be inherited and you will get a compile-time error.
However, if you have compiled your class, there will be no compile-time errors. At this point, Java 8 is inconsistent. It has its own reasons, for various reasons. I don’t want to explain in detail or discuss it in depth here (because: the version has been released and the discussion has been too long, and this platform has never discussed it like this).
The class can run normally in the above case. However, it cannot be recompiled with modified interfaces, but it can still run with old interfaces. Next
When both interfaces provide default implementations to the same method, this method cannot be called unless the implementation class also implements the default method (either directly implements it or inherits the higher-level class on the tree for implementation).
However, this class is compatible. It can be loaded with new interfaces, or even executed, as long as it does not call a method that has default implementation in both interfaces.
Example code
To demonstrate the above example, I created a test directory for C.java, which has 3 subdirectories below, which are used to store I1.java and I2.java. The test directory contains the source code of C.java of class C. The base directory contains the interface to the version that can be compiled and run. I1 contains the m() method implemented by default, and I2 does not contain any methods.
The implementation class contains the main method, so we can execute it in the test. It will check whether there are command line parameters, so that we can easily execute tests that call m() and not call m().
public class C implements I1, I2 { public static void main(String[] args) { C c = new C(); if(args.length == 0 ){ cm(); } }}public interface I1 { default void m(){ System.out.println("hello interface 1"); }}public interface I2 {}Use the following command line to compile and run:
javac -cp .:base C.javajava -cp .:base Chello interface 1
The compatible directory contains the I2 interface with the abstract method m() and the unmodified I1 interface.
public interface I2 { void m();}This cannot be used to compile class C:
javac -cp .:compatible C.javaC.java:1: error: C is not abstract and does not override abstract method m() in I2public class C implements I1, I2 { ^ 1 errorThe error message is very accurate. Because we have the C.class obtained from the previous compilation, if we compile the interface in the compatible directory, we will still get two interfaces that can run the implementation class:
javac compatible/I*.javajava -cp .:compatible Chello interface 1
The third directory called wrong, and the I2 interface contained also defines the m() method:
public interface I2 { default void m(){ System.out.println("hello interface 2"); }}We should never tire of compiling it. Although the m() method is defined twice, the implementation class can still run as long as it does not call the method that has been defined multiple times, but as long as we call the m() method, it will fail immediately. Here are the command line parameters we use:
javac wrong/*.javajava -cp .:wrong C Exception in thread "main" java.lang.IncompatibleClassChangeError: Conflicting default methods: I1.m I2.m at Cm(C.java) at C.main(C.java:5)java -cp .:wrong C x
in conclusion
When you port the class library that adds default implementation to the interface to the Java 8 environment, there will generally be no problem. At least this is what the Java8 class library developers think when adding default methods to collection classes. Applications that use your class library still rely on Java7's class library without the default method. When using and modifying multiple different class libraries, there is a small chance of conflict. How can I avoid it?
Design your class library as before. Don't take it lightly when you may rely on the default method. Do not use it as absolutely necessary. Choose the method name wisely to avoid conflicts with other interfaces. We will learn how to use this feature for development in Java programming.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.