Java call base method from base constructor

前端 未结 3 2002
北荒
北荒 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 04:00

    You can't - it's a method which has been overridden in the subclass; you can't force a non-virtual method call. If you want to call a method non-virtually, either make the method private or final.

    In general it's a bad idea to call a non-final method within a constructor for precisely this reason - the subclass constructor body won't have been executed yet, so you're effectively calling a method in an environment which hasn't been fully initialized.

提交回复
热议问题