编译的原因
interface Furniture {}class Test { public static void main(String[] args) { Furniture f; Cat cat = new Cat(); f = (Furniture)cat; //runtime error }}是你可能会拥有
public class CatFurniture extends Cat implements Furniture {}如果创建
CatFurniture实例,则可以将其分配给,
Catcat并且可以将该实例强制转换为
Furniture。换句话说,某些
Cat子类型可能会实现该
Furniture接口。
在第一个例子中
class Test { public static void main(String[] args) { Chair chair = new Char(); Cat cat = new Cat(); chair = (Chair)cat; //compile error }}除非自身从
Cat扩展,
Chair否则某些子类型不可能
Cat扩展
Chair。



