title: Builder Pattern and Inheritance
body: <p>I have an object hierarchy that increases in complexity as the inheritance tree deepens. None of these are abstract, hence, all of their instances serve a, more or less sophisticated, purpose.</p>
  
  <p>As the number of parameters is quite high, I would want to use the Builder Pattern to set properties rather than code several constructors. As I need to cater to all permutations, leaf classes in my inheritance tree would have telescoping constructors.</p>
  
  <p>I have browsed for an answer here when I hit some problems during my design. First of, let me give you a simple, shallow example to illustrate the problem.</p>
  
  <pre><code>public class Rabbit
  {
      public String sex;
      public String name;
  
      public Rabbit(Builder builder)
      {
          sex = builder.sex;
          name = builder.name;
      }
  
      public static class Builder
      {
          protected String sex;
          protected String name;
  
          public Builder() { }
  
          public Builder sex(String sex)
          {
              this.sex = sex;
              return this;
          }
  
          public Builder name(String name)
          {
              this.name = name;
              return this;
          }
  
          public Rabbit build()
          {
              return new Rabbit(this);
          }
      }
  }
  
  public class Lop extends Rabbit
  {
      public float earLength;
      public String furColour;
  
      public Lop(LopBuilder builder)
      {
          super(builder);
          this.earLength = builder.earLength;
          this.furColour = builder.furColour;
      }
  
      public static class LopBuilder extends Rabbit.Builder
      {
          protected float earLength;
          protected String furColour;
  
          public LopBuilder() { }
  
          public Builder earLength(float length)
          {
              this.earLength = length;
              return this;
          }
  
          public Builder furColour(String colour)
          {
              this.furColour = colour;
              return this;
          }
  
          public Lop build()
          {
              return new Lop(this);
          }
      }
  }
  </code></pre>
  
  <p>Now that we have some code to go on, imaging I want to build a <code>Lop</code>:</p>
  
  <pre><code>Lop lop = new Lop.LopBuilder().furColour("Gray").name("Rabbit").earLength(4.6f);
  </code></pre>
  
  <p>This call will not compile as the last chained call cannot be resolved, <code>Builder</code> not defining the method <code>earLength</code>. So this way requires that all calls be chained in a specific order which is very impractical, especially with a deep hierarchy tree.</p>
  
  <p>Now, during my search for an answer, I came across <a href="https://stackoverflow.com/questions/17164375/subclassing-a-java-builder-class">Subclassing a Java Builder class</a> which suggests using the <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=133275" rel="noreferrer">Curiously Recursive Generic Pattern</a>. However, as my hierarchy does not contain an abstract class, this solution will not work for me. But the approach relies on abstraction and polymorphism to function which is why I don't believe I can adapt it to my needs.</p>
  
  <p>An approach I have currently settled with is to override all methods of the superclass <code>Builder</code> in the hierarchy and simply do the following:</p>
  
  <pre><code>public ConcreteBuilder someOverridenMethod(Object someParameter)
  {
      super(someParameter);
      return this;
  }
  </code></pre>
  
  <p>With this approach I can assure I am being returned an instance I can issue chain calls on. While this is not as worse as the Telescoping Anti-pattern, it is a close second and I consider it a bit "hacky".</p>
  
  <p>Is there another solution to my problem that I am not aware of? Preferably a solution consistent with the design pattern. Thank you!</p>