Java 8 default method details
Java 8 default method details
Author: chszs, reprinted with note. Blog homepage: http://blog.csdn.net/chszs
Java 8 adds the default method, which can add new features to the interface without affecting the interface implementation class. The following is an example to illustrate this point.
public class MyClass implements InterfaceA {public static void main(String[] args){}@Overridepublic void saySomething() {// TODO Auto-generated method stub}}interface InterfaceA{public void saySomething();}The code above shows that the MyClass class implements the saySomething () method of the InterfacesA interface. Now we add a sayHi () method for the InterfacesA interface. In this case, the MyClass class cannot be compiled unless we provide the sayHi () implementation method.
The Default method is very useful. By adding the keyword default before the access modifier of the method defined by the interface, the implementation class does not need to provide the implementation of this method. For example:
public class MyClass implements InterfaceA {public static void main(String[] args){}@Overridepublic void saySomething() {// TODO Auto-generated method stub}}interface InterfaceA{public void saySomething();default public void sayHi(){System.out.println("Hi");}}Note that we must provide the implementation of all default methods. Therefore, the default method makes our code more flexible and can be implemented by writing methods in the interface. The implementation method is implemented as the default method.
So what should I do if there are conflicts between multiple interfaces?
Because Java classes can implement multiple interfaces, there may be a situation where two or more interfaces have a default Interface Method with the same name, resulting in a conflict. Because when the Java virtual machine is running, it is not clear which default method you want to use. This will cause compilation errors.
Let's take a look at the example below.
public class MyClass implements InterfaceA, InterfaceB {public static void main(String[] args){MyClass mc = new MyClass();mc.sayHi();}@Overridepublic void saySomething() {// TODO Auto-generated method stub}}interface InterfaceA{public void saySomething();default public void sayHi(){System.out.println("Hi from InterfaceA");}}interface InterfaceB{default public void sayHi(){System.out.println("Hi from InterfaceB");}}It is not compiled, and the following error is reported:
"Duplicate default methods named sayHi with the parameters () and () are inherited from the types InterfaceB and InterfaceA ."
Unless the sayHi () method is rewritten in the MyClass class:
public class MyClass implements InterfaceA, InterfaceB {public static void main(String[] args){MyClass mc = new MyClass();mc.sayHi();}@Overridepublic void saySomething() {// TODO Auto-generated method stub}@Overridepublic void sayHi(){System.out.println("implemetation of sayHi() in MyClass");}}interface InterfaceA{public void saySomething();default public void sayHi(){System.out.println("Hi from InterfaceA");}}interface InterfaceB{default public void sayHi(){System.out.println("Hi from InterfaceB");}}If you want to specify the sayHi () method of which interface to call, we can do this:
public class MyClass implements InterfaceA, InterfaceB {public static void main(String[] args){MyClass mc = new MyClass();mc.sayHi();}@Overridepublic void saySomething() {// TODO Auto-generated method stub}@Overridepublic void sayHi(){InterfaceA.super.sayHi();}}interface InterfaceA{public void saySomething();default public void sayHi(){System.out.println("Hi from InterfaceA");}}interface InterfaceB{default public void sayHi(){System.out.println("Hi from InterfaceB");}}Is the answer simple?