how to override a method in java and then call the superclass method

只愿长相守 提交于 2019-12-02 20:29:44

问题


I am trying to override a method in the superclass in my java subclass and then call the overriden method from the superclass. Is this the correct way to do it. The method in the super class i want to overrride is called describe()

 public static void describe()
    {
        Item.describe();



    }

回答1:


Use the super keyword.

@Override
public void describe() {
    super.describe();
}

Note the annotation which although is not necessary is "correct".

As was pointed out, static methods do not get inherited. It's possible to imitate polymorphic behavior with static methods using reflection but I do not recommend this for a beginner. It's just something that you can do. You can create identical static method signatures in the subclasses and use Method.invoke() on a superclass reference and functionally it is the same as overriding a static method.



来源:https://stackoverflow.com/questions/19575552/how-to-override-a-method-in-java-and-then-call-the-superclass-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!