I have a problem in understanding Question no.9 from Chapter Object Orientation from the SCJP book by K&B.
Question:
public class Redwood extends Tree {
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 tl, Redwood rl) {
Redwood r2 = (Redwood) tl;
Tree t2 = (Tree)rl;
}
}
class Tree { }
Options:
What is the result? (Choose all that apply.)
A. An exception is thrown at runtime
B. The code compiles and runs with no output
C. Compilation fails with an error at line 8
D. Compilation fails with an error at line 9
E. Compilation fails with an error at line 12
F. Compilation fails with an error at line 13
The answer given in the book is A because Tree cannot be downcast to Redwood. I am just having problem to understand the concept.