Here is my sample code:
public class Bar extends Foo {
}
@SuppressWarnings("unchecked")
public class Foo {
public static void main(String[] args) {
Foo foo = new Bar();
Class<Foo> x = (Class<Foo>) foo.getClass();
System.out.println(x);
}
}
Well I am expecting this programs output to be: Foo.class since I am casting to Class however I see in output:
class Bar
Why?
If I remove the casting part from the code, program will not even compile:
java: incompatible types
required: java.lang.Class<Foo>
found: java.lang.Class<capture#1 of ? extends Foo>
Why is the compiler forcing me to cast?