<pre>
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: public 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 //ANS
C. Compile time error at line 17
D. NO ERRORS
</pre>
My thinking:
line 17 and 18 should not give any Run Time error since
d1 and
d2 are instances of the derived classes, which are inherited from class
Base.
And one more question:
Does lines 17 and 18 same as follows:
b = d1;
b = d2;
And I know:
d1 = (Derived1)b; //will give run time error
d2 = (Derived2)b; //will give run time error
Thanx in advance.
[This message has been edited by Umesh (edited April 14, 2000).]