Eclipse/javac disagree on compiling signature with default method collision; who is right?

前端 未结 4 705
-上瘾入骨i
-上瘾入骨i 2020-12-03 13:54

Here\'s a simple class that demonstrates the issue:

package com.mimvista.debug;

public class DefaultCollisionTest {
    public static interface Interface1 {         


        
4条回答
  •  醉酒成梦
    2020-12-03 14:35

    Eclipse is right.

    I have not found this javac bug in the Java Bug Database and therefore reported it: JDK-8186643

    Better explanation by Stephan Herrmann (see his comment below):

    Right, reporting an error against an intersection type should only happen when the intersection type is not well-formed and hence the intersection is empty. But as this answer shows, the intersection is not empty and should thus be legal. Actually, the error message class INT#1 inherits ... makes no sense, because at that point nobody mentioned a class INT#1, we only have the intersection of two interfaces, and that intersection is used only as a bound, not as a type.

    A class that implements multiple interfaces of the same method can be compiled with both compilers, even if the method of one interface has a default implementation. The class can be referenced as as long as neither I1 nor I2 has a default implementation for a equally named method. Only if one of the two interfaces has a default implementation javac fails.

    In case of ambiguity which implementation should apply, the error should already occur when defining a class, not when the class is referred as (see JLS 4.9. Intersection Types).

    See following example which works with and , but fails with and javac:

    interface I1 {
        String get();
    }
    
    interface I2 {
        String get();
    }
    
    interface IDefault {
        default String get() {
            return "default";
        };
    }
    
    public class Foo implements I1, I2, IDefault {
    
        @Override
        public String get() {
            return "foo";
        }
    
        public static void main(String[] args) {
            System.out.print(getOf(new Foo()));
        }
    
    //  static  String getOf(T t) { // fails with javac
        static  String getOf(T t) { // OK
            return t.get();
        }
    
    }
    

提交回复
热议问题