Constructors calling overridable methods
807588Mar 17 2009 — edited Mar 17 2009Hi,
In C++, if a constructor calls a virtual function, compile-time binding is used - the version of the function called is the version of the function in the class where the constructor is defined, never a sub-class function. In Java, calling a non-final method in a constructor can actually call an overriding method in a sub-class, even though the sub-class object is not yet initialized.
Why is Java defined this way? Is there any advantage to this that is commonly used? It seems like the C++ behavior is a lot more rational - since the sub-class object is not yet initialized, its functions will access uninitialized member values.
Thanks,
Shlomy