10.8 A Deeper Explanation of Issues with Calling Methods from Constructors

Do not call overridable methods from constructors. When creating a subclass object, this could lead to an overridden method being called before the subclass object is fully initialized.

Recall that when you construct a subclass object, its constructor first calls one of the direct superclass’s constructors. If the superclass constructor calls an overridable method, the subclass’s version of that method will be called by the superclass constructor—before the sub-class constructor’s body has a chance to execute. This could lead to subtle, difficult-to-detect errors if the subclass method that was called depends on initialization that has not yet been performed in the

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.