tags:

views:

89

answers:

1

In the following code, MoreSpecificHasInfo.getInfo() is not permitted as an override by the Sun Java 5/6 compilers, saying, "types IHasSpecialInfo and AbstractHasInfo are incompatible; both define getInfo(), but with unrelated return types", although it compiles without error under Eclipse 3.4.2. Also, if I replace the abstract implementation of getInfo() with the commented concrete version, it compiles fine everywhere.

I'm wondering which (if any) of the Sun or Eclipse compilers are in line with the Java Language Spec, and if the Sun's compiler is correct, why is this not permitted?

interface IInfo {

}

interface ISpecialInfo extends IInfo {

}

interface IHasInfo {

    IInfo getInfo();

}

interface IHasSpecialInfo extends IHasInfo {

    ISpecialInfo getInfo();

}

abstract class AbstractHasInfo implements IHasInfo {

    public abstract AbstractInfo getInfo();

    abstract class AbstractInfo implements IInfo {

    }

}

abstract class MoreSpecificHasInfo extends AbstractHasInfo implements IHasSpecialInfo {

    @Override
    abstract public MoreSpecificInfo getInfo();

    //    @Override
    //    public MoreSpecificInfo getInfo() {
    //        return null;
    //    }

    abstract class MoreSpecificInfo extends AbstractHasInfo.AbstractInfo implements ISpecialInfo {

    }
}
+2  A: 

If I understood the issue correctly, it's a bug in Java. The bug status says that a fix is delivered but it's not at least in Java 6.

Esko
I believe that the bug database is saying that the fix is in the JDK 7 beta in build 31.
Stephen C
I think the bug to which you refer is concerned with interfaces. The interfaces in @Matt R's example compile.
akf
Thanks, yes, just tried this in the JDK 7 snapshot and it's been fixed. Certainly, the proposed solution in the bug evaluation covers this example too, "javac should be modified so that if a given pair of supertypes (Sj, Sk) indeed contains a pair of incompatible member (m1,m2), the site symbol should be checked for a symbol m3 overriding both m1 and m2."
Matt R