The following code snippet will result in a run-time:
class Vehicle {
public void printSound() {
System.out.print("vehicle");
}
}
class Car extends Vehicle {
public void printSound() {
System.out.print("car");
}
}
class Bike extends Vehicle {
public void printSound() {
System.out.print("bike");
}
}
public class Test {
public static void main(String[] args) {
Vehicle v = new Car();
Bike b = (Bike) v;
v.printSound();
b.printSound();
}
}
My question is: why does that result in a run-time error but not a compilation error? Shouldn’t the compiler know that ‘v’ is already a ‘Car’ and cannot be cast into a ‘Bike’?
In theory, it would be possible for a compiler to say to itself: “
vis local variable, that is assigned to be aCar. At no point prior to the attempted cast toBikedoes it change its value, and there is no way forCarto be successfully cast toBike. Therefore, this is an error.”However, I know of no Java compiler that will do that analysis for you. It’s really only worthwhile in the simplest of cases. Instead, the behavior is that the compiler sees the cast, and reasons that it is possible to cast a
Vehicleto aBike, and so it allows it.In general, that’s what a cast means: it tells the compiler that even though this assignment might fail, you’re pretty certain that it won’t. In exchange for allowing the code to compile, you assume the risk of a run-time exception.