Given the following class
public class Test<T extends Test.UpperClass> {
public static class BottomClass {}
public static class UpperClass extends BottomClass {}
public void method(BottomClass c) {
System.out.println("BottomClass " + c.getClass());
}
public void method(T c) {
System.out.println("UpperClass " + c.getClass());
}
public void test() {
method(new UpperClass());
}
public static void main(String[] args) {
new Test<UpperClass>().method(new UpperClass()); // calls method(T)
new Test<UpperClass>().test(); // calls method(BottomClass)
}
}
The code above if executed, will print out UpperClass class Test$UpperClass and BottomClass class Test$UpperClass, which is not what I expected as T extends UpperClass, I would expect both will call method(T) as the erasure of T is UpperClass. Why method(BottomClass) is called instead?
UpperClasswhereTis expected. Why would the erasure matter?