In this example, why is it still necessary to typecast an object to be sure of its type, even after getClass() has been called?

前端 未结 4 1068
长发绾君心
长发绾君心 2021-01-28 16:20

I\'m following this MOOC on OOP in Java and it has presented an example I don\'t fully understand. In the example, a Book class has been created and they are const

4条回答
  •  旧时难觅i
    2021-01-28 17:15

    You have to cast your Object-type object to Book, because you want to use getPublishingYear() and getName() functions which are specific to the Book class. The Object class does not have such methods.

提交回复
热议问题