Casting to Superclass, and Calling Overriden Method

前端 未结 4 1799
梦如初夏
梦如初夏 2021-01-23 16:44

I have my next question. I have extended a class, Parrent and overridden one of its method in the Child class. I tried to cast the type to the supercla

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-23 17:33

    Method resolution occurs at runtime, not compile time. The object's class (its behaviour) declares an implementation of the method, so that is used.

    Being able to refer to objects by their superclass is the essence of OOP.
    See Liskov substitution principle

提交回复
热议问题