Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apparent type violation, but compiles [duplicate]

Tags:

java

generics

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();
    }
}
like image 895
Jerome Avatar asked Jul 03 '17 14:07

Jerome


1 Answers

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.

like image 53
GhostCat Avatar answered Oct 27 '22 07:10

GhostCat