Forum Stats

  • 3,728,151 Users
  • 2,245,559 Discussions
  • 7,853,353 Comments

Discussions

Inheritance for default and abstract methods from generic interface

3049833
3049833 Member Posts: 1
edited October 2015 in Java 8 Questions

According to https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.4.1.3 (Inheriting Methods with Override-Equivalent Signatures)

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 following code compiles (javac 1.8.0_60) without errors. Is this a correct behaviour?

interface A {
    void foo(String x);
}

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

interface I extends A, B<String> { } // No errors.

Answers

  • Unknown
    edited October 2015
    According to https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.4.1.3 (Inheriting Methods with Override-Equivalent Signatures)
    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 following code compiles (javac 1.8.0_60) without errors. Is this a correct behaviour?
    interface A {
        void foo(String x);
    }
    
    interface B<T> extends A {
        default void foo(T x) {}
    }
    
    interface I extends A, B<String> { } // No errors.
    

    No - I don't believe it is.

    The second 'foo' method IS override-equivalent with the first 'foo' method as defined in section 8.4.2 of that same spec.

    8.4.2. Method Signature

    Two methods or constructors, M and N, have the same signature if they have the same name, the same type parameters (if any) (§8.4.4), and, after adapting the formal parameter types of N to the the type parameters of M, the same formal parameter types. 
    The signature of a method m<sub>1</sub> is a subsignature of the signature of a method m<sub>2</sub> if either: 
    
      m<sub>2</sub> has the same signature as m<sub>1</sub>, or  
      the signature of m<sub>1</sub> is the same as the erasure (§4.6) of the signature of m<sub>2</sub>Two method signatures m<sub>1</sub> and m<sub>2</sub> are override-equivalent iff either m<sub>1</sub> is a subsignature of m<sub>2</sub> or m<sub>2</sub> is a subsignature of m<sub>1</sub>. 
    It is a compile-time error to declare two methods with override-equivalent signatures in a class. 
    

    Bullet #2 applies: the first method signature has the same erasure as the signature of the second method and hence is a 'subsignature' of the signature of the second method (obviously a String can be a subset of the T type specifier).

    Since one is a 'subsignature' then they are 'override-equivalent'.

    If you have a MOS account I suggest you report a bug to Oracle.

    .

    3049833
Sign In or Register to comment.