The Java tutorial on type erasure doesn\'t seem to detail the specific rules of cast insertion by the compiler. Can someone please explain the specific rules that cause the
The ClassCastException would be throw when a call is made to
n.setData("Hello");
This is because the compiler builds bridge methods to preserve polymorphism. The bridge method will look like:
public void setData(Object data) {
setData((Integer)data); //the exception is thrown here
}
since an instance of a string cannot be converted to Integer, the ClassCastException would be thrown.
You can read about bridge methods here.