I don't understand why the casting from bellow doesn't work ( Helicopter h = (Helicopter) new Rotorcraft();
) and throws
a Runtime exception
of type ClassCastException
.
Base class:
public class Rotorcraft {
protected final int height = 5;
protected int fly(){
return height;
}
}
Child class:
public class Helicopter extends Rotorcraft {
private int height = 10;
public int fly() {
return super.height;
}
public static final void main(String[] a){
Helicopter h = (Helicopter) new Rotorcraft();
}
}