Can someone help me understand why the code below compiles:
class Main {
public static void main(String args[]) {
Concrete c = foo();
}
public static <T extends Interface> T foo() {
return null;
}
interface Interface {}
static class Concrete {}
}
In my mind, Concrete c = foo() should not compile since T is bound to Interface only and not guaranteed to be Concrete.
If I switch null to (T) new Interface() {} it sill compiles with unchecked warning (on the return expression inside foo(), not on Concrete c = foo()). Obviously it gives me a ClassCastException when executed.