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

后端 未结 1 1225
后悔当初
后悔当初 2021-01-28 07:16

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 th

相关标签:
1条回答
  • 2021-01-28 07:24

    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.

    0 讨论(0)
提交回复
热议问题