title: Extending Builder Class
body: <p>I want to implement a design pattern where I setup a base builder class (BaseBuilder.java) that several other builder classes extend.</p>
  
  <p>The base builder class is abstract, and it contains values that are common to the other builders. Each builder shares some common elements and also has unique elements.</p>
  
  <p>BaseBuilder Class Example-</p>
  
  <pre><code>public class BaseBuilder {
  
      private final String key;
      private final String token;
  
      public BaseBuilder(Builder&lt;?&gt; builder) {
          this.key = builder.key;
          this.token = builder.token;
      }
  
      @Override
      public String toString() {
          return "Builder {" +
              ", key='" + key + '\'' +
              ", token='" + token + '\'' +
              '}';
      }
  
      public abstract static class Builder&lt;T extends Builder &lt;T&gt;&gt; {
  
          private String key;
          private String token;
  
          public Builder setKey(String key) {
              this.key = key;
              return this;
          }
  
          public Builder setToken(String token) {
              this.token = token;
              return this;
          }
  
          public BaseBuilder build() {
              return new BaseBuilder(this);
          }
      }
  }
  </code></pre>
  
  <p>Now, I would like to extend this BaseBuilder for other builders e.g. RedCar.</p>
  
  <pre><code>public class RedCar extends BaseBuilder {
  
      private final String redWheelCaps;
  
      public RedCar(RedCardBuilder builder) {
          super(builder);
          this.redWheelCaps = builder.redWheelCaps;
      }
  
      @Override
      public String toString() {
          return "Red Car Builder {" +
              ", redWheelCaps='" + redWheelCaps + '\'' +
              '}';
      }
  
      public static class RedCardBuilder extends BaseBuilder&lt;RedCardBuilder&gt; {
  
          private String redWheelCaps;
  
          public RedCardBuilder setRedWheelCaps(String redWheelCaps) {
              this.redWheelCaps = redWheelCaps;
              return this;
          }
  
  
          public RedCar build() {
              return new RedCar(this);
          }
      }
  }
  </code></pre>
  
  <p>When attempting to use this builder, I am having issues trying to achieve the following results.</p>
  
  <pre><code>// this is ideally what I want to achieve
  // there is an error because I am not creating a BaseBuilder object
  RedCar redCar = new RedCar.RedCarBuilder()
      .setKey("key")
      .setToken("token")
      .setRedWheelCaps("redCaps")
      .build();
  </code></pre>
  
  <p>The above does not work. I am being prompted that I have to use the BaseBuilder. So I updated my code to the following:</p>
  
  <pre><code>// this works good, but the order of my set values causes an error
  BaseBuilder redCar = new RedCar.RedCarBuilder()
      .setKey("key")
      .setToken("token")
      .setRedWheelCaps("redCaps")
      .build();
  </code></pre>
  
  <p>I was forced to use Polymorphism. Which is fine, however, I still had an error until I rearranged how I set the values. For some reason, I have to set the values of RedCar first, before I can set the values of BaseBuilder. Otherwise I receive the error, "cannot resolve method". So I rearranged my set values to remove all errors.</p>
  
  <pre><code>// no errors at all. This works fine but is not what I was expecting
  BaseBuilder redCar = new RedCar.RedCarBuilder()
      .setRedWheelCaps("redCaps")
      .setKey("key")
      .setToken("token")
      .build();
  </code></pre>
  
  <p>How can I fix my setup so that I can inherit and set values without having to conform to a specific order? And is there anyway of accomplishing what I am trying to do without having to use Polymorphism? Do I have the correct pattern idea? Thanks!</p>
  
  <p>EDIT:
  More information is that I can achieve what I am seeking so long that I omit the setters from BaseBuilder. So, the following is OK.</p>
  
  <pre><code>RedCar redCar = new RedCar.RedCarBuilder()
      .setRedWheelCaps("redCaps")
      .build();
  </code></pre>
  
  <p>How come it seems like I am unable to inherit the setters from my BaseBuilder?</p>