Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting methods with override-equivalent signatures

Tags:

java

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?

like image 464
Sachin Sachdeva Avatar asked Sep 26 '17 06:09

Sachin Sachdeva


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.

like image 186
Luciano van der Veekens Avatar answered Oct 15 '22 09:10

Luciano van der Veekens