We all know that you can only define method names in the interface of the Java language, not the specific implementation code of the method. The methods defined in the interface must be implemented in the non-abstract subclasses of the interface. Here is an example of an interface:
| 123456789101112131415 |
public interface SimpleInterface { public void doSomeWork();}class SimpleInterfaceImpl implements SimpleInterface{ @Override public void doSomeWork() { System.out.println("Do Some Work implementation in the class"); } public static void main(String[] args) { SimpleInterfaceImpl simpObj = new SimpleInterfaceImpl(); simpObj.doSomeWork(); }} |
So what happens if we add a new method to the Simpleinterface?
| 1234 |
publicinterface SimpleInterface { public void doSomeWork(); public voiddoSomeOtherWork();} |
If we try to compile the code above, we will get the following result:
| 123456 |
$javac .\SimpleInterface.java.\SimpleInterface.java:18: error: SimpleInterfaceImpl is not abstract and does not override abstract method doSomeOtherWork() inSimpleInterfaceclass SimpleInterfaceImpl implements SimpleInterface{^1 error |
Because the interface has this syntax constraint, it is very difficult to directly change/extend the methods within the interface. When we tried to harden the Java 8 collections API to support lambda expressions, we faced the challenge. To overcome this difficulty, Java 8 introduces a new concept called the default method, which can also be referred to as the Defender method, or virtual extension method (Vsan extension methods).
The default method refers to the inclusion in the interface of some of the defaults of the implementation of the method (that is, the interface can contain the method body, which breaks the Java version of the interface syntax limitations), so that the interface in the extension of the time, will not break the interface-related implementation class code. Next, let's look at an example:
| 123456789101112131415161718192021222324252627 |
public interface SimpleInterface { public void doSomeWork(); //A default method in the interface created using "default" keyword //使用default关键字创在interface中直接创建一个default方法,该方法包含了具体的实现代码 default public void doSomeOtherWork(){ System.out.println("DoSomeOtherWork implementation in the interface"); }}class SimpleInterfaceImpl implements SimpleInterface{ @Override public void doSomeWork() { System.out.println("Do Some Work implementation in the class"); } /* * Not required to override to provide an implementation * for doSomeOtherWork. * 在SimpleInterfaceImpl里,不需要再去实现接口中定义的doSomeOtherWork方法 */ public static void main(String[] args) { SimpleInterfaceImpl simpObj = new SimpleInterfaceImpl(); simpObj.doSomeWork(); simpObj.doSomeOtherWork(); }} |
The output of this program is:
| 12 |
Do Some Work implementation inthe classDoSomeOtherWork implementation inthe interface |
The above is a very brief introduction to the default method. If you are interested, you can also read this document to get a deeper understanding.
Update:
One of the most common questions to ask is: If a class implements two interfaces (which can be seen as "multiple inheritance"), and both interfaces contain a default method with the same name, what happens? In this case, the compiler will make an error. Let me use an example to explain:
| 1234567891011 |
public interface InterfaceWithDefaultMethod { public void someMethod(); default public void someOtherMethod(){ System.out.println("Default method implementation in the interface"); }}public interface InterfaceWithAnotherDefMethod { default public void someOtherMethod(){ System.out.println("Default method implementation in the interface"); }} |
Then we define a class that implements the above two interfaces:
| 12345678910111213 |
public class DefaultMethodSample implements InterfaceWithDefaultMethod, InterfaceWithAnotherDefMethod{ @Override public void someMethod(){ System.out.println("Some method implementation in the class"); } public static void main(String[] args) { DefaultMethodSample def1 = new DefaultMethodSample(); def1.someMethod(); def1.someOtherMethod(); } } |
If you compile the above code, you will get a compiler error, as shown below. Because the compiler does not know which one to choose in the two default methods with the same name, it generates two semantics.
Original link: Blog.sanaulla.info translation: importnew.com-Huang
Link: http://www.importnew.com/7302.html
[ Reprint please keep the source, translator and translation links.] ]
Java 8 new Features--default method (Defender method) Introduction