posted 15 years ago
The following is from a Whizlabs Diagnostic Exam
Just to focus on the problem, the official answer given says that there is
no ClassCastException at runtime.
I would think that there is and I'll explain why I think so.
Line1 (ArrayList b = a) is taking an ArrayList<Circle> and assigning it
to an ArrayList raw reference. That I can live with.
The next line casts b to an ArrayList<Rectangle> and puts it into c, an ArrayList<Rectangle> reference. So I have taken what is actually an
ArrayList<Circle> and stuffed it into an ArrayList<Rectangle> reference by
casting it. Fine! I can see that would get through the compiler but, at
runtime, I would think that the JVM would realize that the true type of b is an ArrayList<Circle> and throw a ClassCastException.
Okay..... what am I missing?
[ September 05, 2008: Message edited by: Bob Ruth ]
------------------------
Bob
SCJP - 86% - June 11, 2009