Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Constructors calling overridable methods

807588Mar 17 2009 — edited Mar 17 2009
Hi,

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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 14 2009
Added on Mar 17 2009
3 comments
390 views