The default method for the Java8 interface

  • 2020-04-01 04:37:51
  • OfStack

The default method for the Java8 interface

What is the default method and why is there a default method?

Simply put, an interface can have implementation methods, and you don't need an implementation class to implement its methods. Simply prefix the method name with the default keyword.

Why this feature? First of all, before the interface is a double-edged sword, benefit is an abstract rather than concrete programming oriented, defect is, when you need to modify the interface, you need to modify all classes implement this interface, a collection of Java 8 before the current frame without the foreach method, can think of a solution is normally in the JDK add new methods and implementation to the relevant interface. However, with a released version, there is no way to add new methods to the interface without affecting the existing implementation. So the default method is introduced. Their goal is to keep the interface from introducing incompatible developments with existing implementations.

As shown below,


public interface Animal {
  default void eat() {
    System.out.println("animal eat default method");
  }
}

Declares an interface with only one default method. And then write a concrete class that implements the 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();
  }
}

You don't have to override the default method in a concrete class, but you do have to implement the abstract method.

Multiple inheritance of default methods
As 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 the method of B
    obj.doSomething();
  }
}

Print result:


hello world from interface B

hello world from interface A

foo from interface A

foo from interface A

c object need do something

Obj. hello() calls the default method in the B interface. At the same time, the default method in the B interface has called the default method in the parent interface.

Let's look at another example, and think about it in multiple inheritance, if you have a default method with the same name, like this,


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 specify the default method for which interface to call as follows,


 D.super.hello();
 E.super.hello();

another Java8 interface default method instance :

Java8 adds the default method of the interface, which means that it can be implemented in the interface. The implementation method is the default implementation, and you can also override the default method in the implementation class of the interface.

The following example:


public class AppInterfaceDefaultMethod {
 
  public static interface DefaultMethodDemo {
    //Defines the default method, preceded by the default keyword, followed by the method declaration and the 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 {
    //Overrides 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 the Java8 interface of the default method in detail, I hope to help you learn.


Related articles: