Java call base method from base constructor

前端 未结 3 2007
北荒
北荒 2020-12-02 03:14

How to call Super::printThree from Super::Super()?
In the example below I call Test::printThree instead.

class Super {
        Super() { 
        printTh         


        
3条回答
  •  星月不相逢
    2020-12-02 03:37

    If Super needs to ensure that printThree() isn't overridden, that method needs to be declared as final. Alternatively, the overriding method in Test can decide to call the overriden method if it chooses:

    @Override
    void printThree() { super.printThree(); }
    

    If the base class doesn't mark a method as final, it's giving up the right not to have deriving classes override it and do as they please.

提交回复
热议问题