I was reading about Generics from the Eleventh Edition of "Java The Complete Reference", specifically the "Run-Time type Comparisons with a Generic Hierarchy" part.
I was confused by this part:
// The following can't be compiled because
// generic type info does not exist at run time.
// if (iOb2 instanceof Gen2<Integer>)
// System.out.println("iOb2 is instance of Gen2<Integer>")
But when I tried to run the code myself it compiled and executed perfectly:
class Gen<T>{}
class Gen2<T> extends Gen<T>{}
public class Tester{
public static void main(String[] args){
Gen2<Integer> obj = new Gen2<Integer>();
System.out.println(obj instanceof Gen2<Integer>);
//output is true
}
}
Now I understand that Type Erasure should cause a compile time error. But I don't understand why my code works.