Hi, I have replied for some similar kind of question in another topic.
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class Mobile
{
public static void main(
String argv[]){
Base b=new Base();
Sub s=(Sub) b; //Line no 10
}
}
Yes! its a Runtime error, coz of not following hierarchy reflection. you can't assign the obj of base class to the derived class object. But u can assign the obj of derived class to the base class object.
Example code for ur prg: No Compiler or runtime ERROR.
Sub _sub = new Sub();
b = _sub;
Sub2 _sub2 = new Sub2();
b = _sub2;
The point is Derived class may have some additional functionality of Base class, So u cant assign the base class object to derived class object.
The compiler would have shown the error if you had not type cast the b to (Sub). the copiler only checks the r.side operand sink with l.side operand in the assignment operator.
This cast incompatability will come to know only at the runtime.
with regards,
Chiyan