Why is this covariant redefinition allowed if the method is concrete but not abstract?

The following code is MoreSpecificHasInfo.getInfo()not permitted as overrides by Sun Java 5/6 compilers, saying " types IHasSpecialInfo and AbstractHasInfo are incompatible; both define getInfo(), but with unrelated return types", although it compiles without errors in Eclipse 3.4.2. Also, if I replace the abstract implementation getInfo()with a commented-out specific version, it compiles everywhere.

I am wondering which (if any) Sun or Eclipse compilers comply with the Java Language Spec , and if the Sun compiler is correct, why is this not allowed?

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 {

    }
}
+3
source share
1 answer

, Java. , , Java 6.

+3

Source: https://habr.com/ru/post/1717145/


All Articles