The default method of Java8 interface
What is the default method and why there is a default method?
Simply put, the interface can have implementation methods, and there is no need to implement the class to implement its methods. Just add the default keyword before the method name.
Why do you need this feature? First of all, the previous interface was a double-edged sword. The advantage was that it was aimed at abstract rather than concrete programming. The disadvantage was that when it was necessary to modify the interface, it was necessary to modify all classes that implemented the interface. The current collection framework before Java 8 did not have a foreach method. The solution that can be thought of is to add new methods and implementations to the related interfaces in the JDK. However, for the published version, it is impossible to add new methods to the interface without affecting the existing implementation. So the default method introduced. Their purpose is to make the interface not introduced incompatible with existing implementations.
As shown below,
public interface Animal { default void eat() { System.out.println("animal eat default method"); }} An interface is declared, and there is only one default method inside. Then write a specific class to implement this interface.
public class Dog implements Animal { public void sayHi() { System.out.println("dog"); } public static void main(String args[]) { Dog dog = new Dog(); dog.eat(); }}In a specific class, it is not necessary to override the default method, but abstract methods must be implemented.
Multiple inheritance of default methods
The code shown below,
public interface A { void doSomething(); default void hello() { System.out.println("hello world from interface A"); } default void foo() { System.out.println("foo from interface A"); }} interface B extends A { default void hello() { System.out.println("hello world from interface B"); A.super.hello(); this.foo(); A.super.foo(); }} class C implements B, A { @Override public void doSomething() { System.out.println("c object need do something"); } public static void main(String args[]) { A obj = new C(); obj.hello();//Call B's method obj.doSomething(); }}Print result:
hello world from interface Bhello world from interface Afoo from interface Afoo from interface Ac object need do something
obj.hello() calls the default method in interface B. At the same time, the default method in interface B calls the default method in parent interface.
Let’s take a look at another example and think about the default method with the same name in multiple inheritance, as shown below.
public interface D { default void hello() { System.out.println("hello world from D"); }} interface E { default void hello() { System.out.println("hello world from E"); }} class F implements D, E { @Override public void hello() { System.out.println("hello world F class"); D.super.hello(); E.super.hello(); } public static void main(String args[]) { F f = new F(); f.hello(); } } We need to formulate the default method of which interface to call as follows:
D.super.hello(); E.super.hello();
Another example of the default method of java8 :
Java8 has added a default method for the interface, which means that there can be implemented in the interface. This implementation method is the default implementation, and you can also rewrite this default method in the interface implementation class.
The following example:
public class AppInterfaceDefaultMethod { public static interface DefaultMethodDemo { //Define the default method, with the default keyword preceded by the default method, followed by the method declaration and method body default void demo(String input) { System.out.println(input); } void doSomething(); } public static class DemoClass implements DefaultMethodDemo { @Override public void doSomething() { System.out.println("do something"); } } public static class DemoClassOverrideDemo implements DefaultMethodDemo { //Rewrite the default method @Override public void demo(String input) { System.out.println("demo " + input + " by override method"); } @Override public void doSomething() { System.out.println("do something"); } } public static void main(String[] args) { DefaultMethodDemo demo = new DemoClass(); demo.demo("abc"); DefaultMethodDemo demoOverride = new DemoClassOverrideDemo(); demoOverride.demo("abc"); }}The above is a detailed introduction to the default method of Java8 interface, and I hope it will be helpful to everyone's learning.