I saw this question in a mock exam. I ran the code and it ran with any errors but the exam answer differs, can someone please explain to me the logic behind it. Or is the answer wrong.
What happens when the following code executes?
1:class Base{
2:// legal code
3:}
4:class Derived1 extends Base{
5: // legal code
6:}
7:class Derived2 extends Base{
8:// legal code
9:}
10

ublic class Test
11:{
12: static public void main(String [] args)
13: {
14: Base b = new Derived1 ();
15: Derived1 d1 = new Derived1();
16: Derived2 d2 = new Derived2();
17: b = (Base) d1;
18: b = (Base) d2;
}
}
A. Compile time error at line 14
B. Run time error at line 18
C. Compile time error at line 17
D. NO ERRORS
Question Help.
B. Run time error at line 18
b is of type Base and points to d1 and d2 is neither so at run
time it fails