Sub topic 15
Pitfall: Calling Overridable Methods from Constructors
You have seen that Java picks an overridden method based on the object's runtime type. That rule holds everywhere, including inside a constructor. A constructor is the special code that runs when you create an object with new. This chapter shows how that can surprise you and how to avoid it.
The Surprise
Here is a superclass whose constructor calls a method. A subclass overrides that method and uses one of its own fields.
class Base {
Base() {
init();
}
void init() {
System.out.println("Base.init");
}
}
class Derived extends Base {
private String message = "ready";
@Override
void init() {
System.out.println("Derived.init: " + message);
}
}
public class Main {
public static void main(String[] args) {
new Derived();
}
}You might expect Derived.init: ready. The program actually prints:
Derived.init: nullWhy It Happens
Java builds an object in a fixed order:
- Every field gets a default value first. For a
Stringthat isnull, for anintit is0, and for abooleanit isfalse. - The superclass constructor runs (
Base()). - The subclass field initializers run. A field initializer is the
= "ready"part of a field declaration. - The rest of the subclass constructor runs.
Base() calls init(). The object is really a Derived, so dynamic dispatch runs Derived.init(). At that moment step 3 has not happened yet, so message is still null.
How to Avoid It
The rule is simple: a constructor should only call methods that cannot be overridden. A method cannot be overridden if it is private, final, or static.
class Base {
Base() {
setUp();
}
private void setUp() {
System.out.println("Base.setUp");
}
}setUp() is private, so no subclass can replace it. Base() always runs this exact code.
If subclasses really need their own setup step, let each class set up its own fields in its own constructor. A subclass constructor runs after its fields are initialized, so it is a safe place for that work.
class Derived extends Base {
private final String message;
Derived() {
super();
this.message = "ready";
System.out.println("Derived ready: " + message);
}
}This prints Base.setUp and then Derived ready: ready.
| Method kind in a constructor | Safe? | Reason |
|---|---|---|
| `private` | Yes | Subclasses cannot override it |
| `final` | Yes | Subclasses cannot override it |
| `static` | Yes | Static methods are not dispatched by runtime type |
| Plain instance method | No | A subclass may override it and see unset fields |
Exercise
In the first example, suppose Derived declared private int count = 5; and init() printed count. What would new Derived() print, and why?
Show answer
It prints Derived.init: 0. The Base constructor calls the overridden init() before Derived's field initializer runs, so count still has its default int value, 0.
Exercise
Name one change to Base that makes sure its constructor always runs Base's own version of init().
Show answer
Mark init() as private or final. Neither kind can be overridden, so the constructor always runs Base's version.