Originally posted by chitts nathan:
fw = new Crane();
fw = (FourWheeler) du;// 4
In the line 4, even thought i am assinging the result of the cast to a object reference of type FourWheeler(fw) , fw is assinged to the object Crane, which is the sibling of Truck.
Be sure to understand the difference of a reference and an object:
ReferenceType something = new Object();
Think of a reference type as a contract between you and the compiler: anything I assign to fw IS A FourWheeler.
Crane extends FourWheeler, Truck extends FourWheeler, therefore Crane IS A FourWheeler, Truck IS A FourWheeler, so the following is legal:
FourWheeler fourw = new FourWheeler();
FourWheeler crane = new Crane();
FourWheeler truck = new Truck();