I have a confusion about this problem in the process of reading Effective Java about the principle of 'Constructors must not invoke overridable methods, directly or indirectly', that can be explained by below code snippet.
public class Hello {
public static void main(String[] args) {
new Sub();
}
}
class Super {
Super() {
foo();
}
void foo() {
System.out.println("Super");
}
}
class Sub extends Super {
Sub() {
foo();
}
@Override
void foo() {
System.out.println("Sub");
}
}
The result is
Sub
Sub
It seems that in the process of initialization, both Super and Sub all invoke Sub's foo() method, the confusion is that for Super, why does it invoke its child's foo() method instead of itself?