Can anyone here please explain to me why I get a java.lang.ClassCastException when downcasting a Parent to a Child?
public class Child extends Parent{
public static void main(String[] args){
new Child().go();
}
void go(){
go2(new Parent(), new Child());
go2((Child) new Parent(), new Child());
}
void go2(Parent p1, Child c1){
Child c2 = (Child)p1;
Parent p2 = (Parent)c1;
}
}
class Tree{}
I have read reference variable casting and searched for examples in the web. Can someone please explain it to me? I really want to understand why it throw exception. Thanks