why subclass reference can't hold superclass object in java

后端 未结 5 478
醉梦人生
醉梦人生 2021-01-19 03:14
class One{

}
class Two extends One{

}
class Main{

      public static void main(String[]  args){
       Two t = new One(); // invalid 

}`
}

I a

5条回答
  •  感动是毒
    2021-01-19 03:44

    Because a dog has all the behaviours of an animal, but something that is only known to be an animal is not guaranteed to have all the behaviours of a dog.

提交回复
热议问题