public static void main(String[] args) {
new Redwood().go();
}
void go() {
go2(new Tree(), new Redwood());
go2((Redwood) new Tree(), new Redwood());
}
void go2(Tree t1, Redwood r1) {
Redwood r2 = (Redwood)t1;
Tree t2 = (Tree)r1;
}
}
Given the class Redwood extends Tree, the main() method is in Redwood and Tree is an empty class with only a default constructor; the answer to this exam question is "A ClassCastException will be thrown when the code attempts to downcast a Tree to a Redwood."
I want to know why the exception is thrown and where. From my understanding you can declare Tree t1 = new Redwood() so why can't I cast a Tree to a Redwood directly?