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

后端 未结 8 2074
梦如初夏
梦如初夏 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-02 12:46

    overriding means redefining a method from the superclass inside a subclass with identical method signature. In your case, the getTyreCost() method has not been overridden, you have not redefined the method in your subclass, so no need to use super.getTyreCost(), only getTyreCost() will do(just like super.getTyreCost() will do the same way). super keyword is used when a method has been overridden, and you want a method call from within your subclass to be implemented in the superclass.

提交回复
热议问题