Apparent type violation, but compiles

Jerome :

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();
    }
}
GhostCat salutes Monica C. :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=432176&siteId=1