If a constructor calls a method that is overridden in a subclass, it can cause the overriding method in the subclass to be called before the subclass has been initialized. This can lead to unexpected results.

Do not call a non-final method from within a constructor if that method could be overridden in a subclass.

In the following example, executing new Sub("test") results in a NullPointerException. This is because the subclass constructor implicitly calls the superclass constructor, which in turn calls the overridden init method before the field s is initialized in the subclass constructor.

To avoid this problem:

  • The init method in the super constructor should be made final or private.
  • The initialization that is performed in the overridden init method in the subclass can be moved to the subclass constructor itself, or delegated to a separate final or private method that is called from within the subclass constructor.
  • J. Bloch, Effective Java (second edition), pp. 89–90. Addison-Wesley, 2008.
  • The Java Tutorials: Writing Final Classes and Methods.