Inheriting methods with override-equivalent signatures

Viewed 183

According to jls-9.4.1.3

If an interface I inherits a default method whose signature is override-equivalent with another method inherited by I, then a compile-time error occurs. (This is the case whether the other method is abstract or default.)

From above description following code should not compile.

However, when I compile this code, its working absolutely fine.

interface A {
    void foo(String s);
}

interface B<T> extends A {
    default void foo(T x) {
    }
}

interface C extends B<String> {
}

Why does it compile?

1 Answers

If an interface I inherits a default method whose signature is override-equivalent with another method inherited by I, then a compile-time error occurs. (This is the case whether the other method is abstract or default.)

The quote refers to the following situation:

interface A {
    void foo(String s);
}

interface B<T> {
    default void foo(T x) {
    }
}

interface C extends A, B<String> {        
}

where C both inherits a default method and another method with the same signature.

In your given situation B#foo already overrides A#foo and hence C only inherits a single method.

Related