How to call Super::printThree from Super::Super()?
In the example below I call Test::printThree instead.
class Super {
Super() {
printThree(); // I want Super::printThree here!
}
void printThree() { System.out.println("three"); }
}
class Test extends Super {
int three = 3
public static void main(String[] args) {
Test t = new Test();
t.printThree();
}
void printThree() { System.out.println(three); }
}
output:
0 //Test::printThree from Super::Super()
3 //Test::printThree from t.printThree()
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.