Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>On invoking overridable method from constructors</h3> <p>Simply put, this is wrong because it unnecessarily opens up possibilities to <em>MANY</em> bugs. When the <code>@Override</code> is invoked, the state of the object may be inconsistent and/or incomplete.</p> <p>A quote from <em>Effective Java 2nd Edition, Item 17: Design and document for inheritance, or else prohibit it</em>:</p> <blockquote> <p>There are a few more restrictions that a class must obey to allow inheritance. <strong>Constructors must not invoke overridable methods</strong>, directly or indirectly. If you violate this rule, program failure will result. The superclass constructor runs before the subclass constructor, so the overriding method in the subclass will be invoked before the subclass constructor has run. If the overriding method depends on any initialization performed by the subclass constructor, the method will not behave as expected.</p> </blockquote> <p>Here's an example to illustrate:</p> <pre><code>public class ConstructorCallsOverride { public static void main(String[] args) { abstract class Base { Base() { overrideMe(); } abstract void overrideMe(); } class Child extends Base { final int x; Child(int x) { this.x = x; } @Override void overrideMe() { System.out.println(x); } } new Child(42); // prints "0" } } </code></pre> <p>Here, when <code>Base</code> constructor calls <code>overrideMe</code>, <code>Child</code> has not finished initializing the <code>final int x</code>, and the method gets the wrong value. This will almost certainly lead to bugs and errors.</p> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/2898422/calling-an-overridden-method-from-a-parent-class-constructor">Calling an Overridden Method from a Parent-Class Constructor</a></li> <li><a href="https://stackoverflow.com/questions/3330390/state-of-derived-class-object-when-base-class-constructor-calls-overridden-method">State of Derived class object when Base class constructor calls overridden method in Java</a></li> <li><a href="https://stackoverflow.com/questions/3342784/using-abstract-init-function-in-abstract-classs-constructor">Using abstract init() function in abstract class’s constructor</a></li> </ul> <h3>See also</h3> <ul> <li><a href="http://findbugs.sourceforge.net/bugDescriptions.html#UR_UNINIT_READ_CALLED_FROM_SUPER_CONSTRUCTOR" rel="noreferrer">FindBugs - Uninitialized read of field method called from constructor of superclass </a></li> </ul> <hr> <h3>On object construction with many parameters</h3> <p>Constructors with many parameters can lead to poor readability, and better alternatives exist.</p> <p>Here's a quote from <em>Effective Java 2nd Edition, Item 2: Consider a builder pattern when faced with many constructor parameters</em>:</p> <blockquote> <p>Traditionally, programmers have used the <em>telescoping constructor</em> pattern, in which you provide a constructor with only the required parameters, another with a single optional parameters, a third with two optional parameters, and so on...</p> </blockquote> <p>The telescoping constructor pattern is essentially something like this:</p> <pre><code>public class Telescope { final String name; final int levels; final boolean isAdjustable; public Telescope(String name) { this(name, 5); } public Telescope(String name, int levels) { this(name, levels, false); } public Telescope(String name, int levels, boolean isAdjustable) { this.name = name; this.levels = levels; this.isAdjustable = isAdjustable; } } </code></pre> <p>And now you can do any of the following:</p> <pre><code>new Telescope("X/1999"); new Telescope("X/1999", 13); new Telescope("X/1999", 13, true); </code></pre> <p>You can't, however, currently set only the <code>name</code> and <code>isAdjustable</code>, and leaving <code>levels</code> 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 multiple <code>boolean</code> and <code>int</code> arguments, which would really make a mess out of things.</p> <p>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?).</p> <p>Bloch recommends using a builder pattern, which would allow you to write something like this instead:</p> <pre><code>Telescope telly = new Telescope.Builder("X/1999").setAdjustable(true).build(); </code></pre> <p>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.</p> <h3>See also</h3> <ul> <li><a href="http://en.wikipedia.org/wiki/Builder_pattern" rel="noreferrer">Wikipedia/Builder pattern</a></li> <li><em>Effective Java 2nd Edition, Item 2: Consider a builder pattern when faced with many constructor parameters</em> (<a href="http://www.codeproject.com/KB/books/EffectiveJava.aspx" rel="noreferrer">excerpt online</a>)</li> </ul> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern">When would you use the Builder Pattern?</a></li> <li><a href="https://stackoverflow.com/questions/2637268/is-this-a-well-known-design-pattern-what-is-its-name/">Is this a well known design pattern? What is its name?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload