Default interface method for abstract superclass

L0laapk3 :

Lets say I have the following structure:

abstract class A {
     abstract boolean foo();
}

interface B {
     default boolean foo() { return doBlah(); }
}

class C extends A implements B {
    //function foo
}

Java will now complain that class C must implement abstract method foo from A. I can work around this problem relatively easy by redefining the function in C and simply calling B.super.foo();.

however I do not understand why the default function from interface B does not forfill this requirement on its own, and I would like to have a better understanding of the underlying mechanics of java.

L0laapk3 :

The answer of Jacob G. inspired me to come up with this solution:

interface Z {
     abstract boolean foo();
}

abstract class A implements Z {

}

interface B extends Z {
     default boolean foo() { return doBlah(); }
}

class C extends A implements B {

}

This way all subclasses of class A are required to define a method foo(), Without requiring every class that implements B to re-implement it.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=440882&siteId=1