Why reference variable of type object must be cast when used as other object type

前端 未结 4 1211
一向
一向 2021-01-03 12:08

Although all classes in Java are sub-classes of Object class, but different from other object types, a reference variable of type Object can\'t be assigned to any other refe

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-03 12:30

    Once you've assigned tiger to an Object reference, the compiler no longer knows that it's actually a Tiger. All it knows is that the tiger object is an Object -- it might be a String, Integer, Fruitcake, or Animal.

    This is exactly how it's supposed to work.

提交回复
热议问题