Use of 'super' keyword when accessing non-overridden superclass methods

后端 未结 8 2097
梦如初夏
梦如初夏 2021-01-02 12:38

I\'m trying to get the hang of inheritance in Java and have learnt that when overriding methods (and hiding fields) in sub classes, they can still be accessed from the super

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-02 12:56

    Technically, the one that's invoked in this case is the inherited version, for which the implementation is actually provided by the parent class.

    There can be scenarios where you must use the super keyword. e.g. if Car had overridden that method, to provide a different implementation of itself, but you needed to invoke the implementation provided by the parent class then you would have use the super keyword. In that case, you could not afford to omit the super keyword because if you did then you would be invoking the implementation provided by the child class itself.

提交回复
热议问题