Apparent type violation, but compiles [duplicate]

早过忘川 提交于 2021-02-07 11:30:10

问题


Why does the below snippet compile ? OtherInterface does not extends Concrete so I would have bet a kidney that this wouldn't compile. But it does.

public class Test {

    public static interface SomeInterface {}

    public static interface OtherInterface{}

    public static class Concrete implements SomeInterface {

       public <T extends Concrete> T getConcrete() {
            return null;
       }
    }

    public static void doStuff() {
        Concrete c = new Concrete();
        OtherInterface iCompile = c.getConcrete();
    }
}

On the other hand, the next snippet does not compile, which is what I expect.

public class Test {

    public static interface SomeInterface {}

    public static class UnrelatedClass{}

    public static class Concrete implements SomeInterface {

       public <T extends Concrete> T getConcrete() {
            return null;
       }
    }

    public static void doStuff() {
        Concrete c = new Concrete();
        UnrelatedClass iCompile = c.getConcrete();
    }
}

回答1:


The difference is here:

public static interface OtherInterface{} ...
OtherInterface iCompile = c.getConcrete();

vs.

public static class UnrelatedClass{} ...
UnrelatedClass iCompile = c.getConcrete();

Meaning: in the first case, you call the method to return an instance of some interface. Interfaces can be any class.

In the second example, you instruct that returned type is of be a specific class! A class which is known, and that does not implement that other interface!

And the error message:

reason: no unique maximal instance exists for type variable T with upper bounds UnrelatedClass,Concrete

is pretty specific here.

In other words: the compiler factors in the left hand side of that assignment - to determine the valid types. And UnrelatedClass can never be a Concrete - because the class UnrelatedClass does not extend Concrete!

Whereas something that is SomeInterface can as well implement OtherInterface.



来源:https://stackoverflow.com/questions/44888090/apparent-type-violation-but-compiles

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!