强制类型转换不能直接将父类对象转换为子类对象吗
在程序中,我将父类的对象强制转换为子类的对象,为什么会报错?既然是强制转换,本来就是从大类型到小类型的转换啊???
package com.imooc; //学习多态 public class Initial { public static void main(String[] args) { // TODO Auto-generated method stub Dog dog = new Dog(); Animal animal = dog;//自动类型提升 向上类型转换 Animal animal2 = new Animal(); Dog dog2 = (Dog)animal2;//向下类型转换 强制类型转换 // Cat cat = (Cat)animal; } } 运行结果: Exception in thread "main" java.lang.ClassCastException: com.imooc.Animal cannot be cast to com.imooc.Dog at com.imooc.Initial.main(Initial.java:22)