I have a Wicket page class that sets the page title depending on the result of an abstract method.
public abstract class BasicPage extends WebPage {
public BasicPage() {
add(new Label("title", getTitle()));
}
protected abstract String getTitle();
}
NetBeans warns me with the message "Overridable method call in constructor", but what should be wrong with it? The only alternative I can imagine is to pass the results of otherwise abstract methods to the super constructor in subclasses. But that could be hard to read with many parameters.
Best Answer
On invoking overridable method from constructors
Simply put, this is wrong because it unnecessarily opens up possibilities to MANY bugs. When the
@Override
is invoked, the state of the object may be inconsistent and/or incomplete.A quote from Effective Java 2nd Edition, Item 17: Design and document for inheritance, or else prohibit it:
Here's an example to illustrate:
Here, when
Base
constructor callsoverrideMe
,Child
has not finished initializing thefinal int x
, and the method gets the wrong value. This will almost certainly lead to bugs and errors.Related questions
See also
On object construction with many parameters
Constructors with many parameters can lead to poor readability, and better alternatives exist.
Here's a quote from Effective Java 2nd Edition, Item 2: Consider a builder pattern when faced with many constructor parameters:
The telescoping constructor pattern is essentially something like this:
And now you can do any of the following:
You can't, however, currently set only the
name
andisAdjustable
, and leavinglevels
at default. You can provide more constructor overloads, but obviously the number would explode as the number of parameters grow, and you may even have multipleboolean
andint
arguments, which would really make a mess out of things.As you can see, this isn't a pleasant pattern to write, and even less pleasant to use (What does "true" mean here? What's 13?).
Bloch recommends using a builder pattern, which would allow you to write something like this instead:
Note that now the parameters are named, and you can set them in any order you want, and you can skip the ones that you want to keep at default values. This is certainly much better than telescoping constructors, especially when there's a huge number of parameters that belong to many of the same types.
See also
Related questions