public class Animal {
public void eat() {}
}
public class Dog extends Animal {
public void eat() {}
public void main(String[] args) {
Animal animal = new Animal();
Dog dog = (Dog) animal;
}
}
赋值 Dog dog = (Dog) animal;
不生成编译错误,但在运行时生成 ClassCastException
。为什么编译器无法检测到这个错误?