As shown in the code below, given a superclass Parent with a method calculate() that is called in Parent's constructor, but also overriden in the class Child, why does the implicit call to Parent's constructor within Child's constructor not call Parent's calculate() instead of Child's calculate()?
class Parent {
Parent() {
calculate();
System.out.println("Empty parent ctor.");
}
void calculate() {
System.out.println("Parent calculating...");
}
}
class Child extends Parent {
Child() {
System.out.println("Empty child ctor.");
}
@Override
void calculate() {
System.out.println("Child calculating...");
}
}
public class ParentConstructor {
public static void main(String [] args) {
Parent child = new Child();
}
}
Output:
Child calculating...
Empty parent ctor.
Empty child ctor.
I would have thought that to correctly construct a Parent object, its constructor should always be called with its own method definitions?