简体   繁体   中英

How to invoke an interface's default implementation of a method in an overriding implementation?

Suppose I have the following code:

interface HumanoidForm {
    default HumanoidForm reproduce() {
        <appropriate code for humanoid form reproduction>
    }
}

class Android extends Machine implements HumanoidForm {
    public HumanoidForm reproduce() {
        <appropriate code for android reproduction> // how to use HumanoidForm's default implementation here?
    }
}

Now suppose "appropriate code for android reproduction" is best described by using "appropriate code for humanoid form reproduction" as a sub-routine. How can I access "appropriate code for humanoid form" from within "appropriate code for android reproduction"? I can think of three ways, but none of them works:

  • Simply invoking reproduce() invokes the overriding implementation.
  • Writing ((HumanoidForm) this).reproduce() still invokes the overriding implementation.
  • Mimicking the re-use of implementations of methods in super classes by overriding methods, one may think of writing super.reproduce(). However, that refers to Machine's implementation of reproduce, which may not even exist.

So it seems there is no way to re-use the code in the default method for overriding. Is that really so?

HumanoidForm.super.reproduce();

Actually, you can choose freely the existing implementation. Let me give you a scenario slightly more complicated than yours. To make things worse, all A , B & C has the same method signature.

interface A {
    default void doWork() {
       System.out.println("Default implementation From A");
    }
}

interface B{
    default void doWork() {
      System.out.println("Default implementation From B");  
    }   
}

class C{
    void doWork(){
        System.out.println("Default implementation From C");
    }       
}

Now, I create a subclass to C which implements A & B:

class Tester extends C implements A, B
{
    @Override public void doWork(){
        A.super.doWork();  //Invoke A's implementation
        B.super.doWork();  //Invoke B's implementation
        super.doWork();    //Invoke C's implementation
    }
}

The output will be:

Default implementation From A
Default implementation From B
Default implementation From C

when you run :

new Tester().doWork();

Take a look at this: https://blog.idrsolutions.com/2015/01/java-8-default-methods-explained-5-minutes/

In particular the section where it says "If we want to specifically invoke one of the sayHi() methods in either InterfaceA or InterfaceB, we can also do as follows:"

public class MyClass implements InterfaceA, InterfaceB {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
}

@Override
public void saySomething() {
    System.out.println("Hello World");
}

@Override
public 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");
}
}

So it seems there is no way to re-use the code in the default method for overriding. Is that really so?

No, you can reuse the code. You can easily test it and you will see that the following code works:

public class Test implements HumanoidForm
{             
    public static void main(String[] args) 
    {       
        new Test().reproduce();         
    }   

    @Override
    public void reproduce(){
        HumanoidForm.super.reproduce();  //Invoking default method
        System.out.println("From implementing class");
    }       
}

interface HumanoidForm {
    default void reproduce() {
       System.out.println("From default interface");
    }
}

OUTPUT:

From default interface
From implementing class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM