title: Subclassing a Java Builder class
body: <p>Give <a href="http://www.drdobbs.com/jvm/creating-and-destroying-java-objects-par/208403883?pgno=2" rel="noreferrer">this Dr Dobbs article</a>, and the Builder Pattern in particular, how do we handle the case of subclassing a Builder? Taking a cut-down version of the example where we want to subclass to add GMO labelling, a naive implementation would be:</p>
  
  <pre><code>public class NutritionFacts {                                                                                                    
  
      private final int calories;                                                                                                  
  
      public static class Builder {                                                                                                
          private int calories = 0;                                                                                                
  
          public Builder() {}                                                                                                      
  
          public Builder calories(int val) { calories = val; return this; }                                                                                                                        
  
          public NutritionFacts build() { return new NutritionFacts(this); }                                                       
      }                                                                                                                            
  
      protected NutritionFacts(Builder builder) {                                                                                  
          calories = builder.calories;                                                                                             
      }                                                                                                                            
  }
  </code></pre>
  
  <p>Subclass:</p>
  
  <pre><code>public class GMOFacts extends NutritionFacts {                                                                                   
  
      private final boolean hasGMO;                                                                                                
  
      public static class Builder extends NutritionFacts.Builder {                                                                 
  
          private boolean hasGMO = false;                                                                                          
  
          public Builder() {}                                                                                                      
  
          public Builder GMO(boolean val) { hasGMO = val; return this; }                                                           
  
          public GMOFacts build() { return new GMOFacts(this); }                                                                   
      }                                                                                                                            
  
      protected GMOFacts(Builder builder) {                                                                                        
          super(builder);                                                                                                          
          hasGMO = builder.hasGMO;                                                                                                 
      }                                                                                                                            
  }
  </code></pre>
  
  <p>Now, we can write code like this:</p>
  
  <pre><code>GMOFacts.Builder b = new GMOFacts.Builder();
  b.GMO(true).calories(100);
  </code></pre>
  
  <p>But, if we get the order wrong, it all fails:</p>
  
  <pre><code>GMOFacts.Builder b = new GMOFacts.Builder();
  b.calories(100).GMO(true);
  </code></pre>
  
  <p>The problem is of course that <code>NutritionFacts.Builder</code> returns a <code>NutritionFacts.Builder</code>, not a <code>GMOFacts.Builder</code>, so how do we solve this problem, or is there a better Pattern to use?</p>
  
  <p>Note: <a href="https://stackoverflow.com/a/10092517/1270789">this answer to a similar question</a> offers up the classes I have above; my question is regarding the problem of ensuring the builder calls are in the correct order.</p>