title: Subclassing builder pattern with a twist in Java
body: <p>I would like to create an abstract Builder for an abstract class (although it does not need to be abstract) and every subclass of the abstract class could have its own subclass Builder. I also want every field/attribute to be filled AKA mandatory. So I use the Builder Patter With a Twist (<a href="https://blog.jayway.com/2012/02/07/builder-pattern-with-a-twist/" rel="nofollow noreferrer">https://blog.jayway.com/2012/02/07/builder-pattern-with-a-twist/</a>).</p>
  
  <p>I encountered a problem which was solved in this question I asked earlier: <a href="https://stackoverflow.com/questions/44506678/generic-parent-object-cannot-be-returned-as-child-without-cast/44509495?noredirect=1#comment76043745_44509495">Generic parent object cannot be returned as child without cast</a>
  But now I am unable to make multiple concrete/subclass Builders.</p>
  
  <p>In the end I would like to instantiate objects something like this:</p>
  
  <pre><code>ConcreteBuilderA.getBuilder().setValue(Object value).setConcreteValue(int num).build()
  </code></pre>
  
  <p>Where setValue() belongs to the AbstractBuilder and the others to the concreteBuilder.</p>
  
  <p>My best shot was (heavily simplified and abstracted):</p>
  
  <pre><code>/**
  * @param&lt;B&gt; the type of builded object it should return.
  * @param&lt;S&gt; the type of the builder subclass.
  * @param&lt;L&gt; the type of the linking interface.
  */
  public abstract class AbstractBuilder&lt;B extends AbstractClass, S extends AbstractBuilder, L&gt; implements ValueSetter&lt;L&gt;
  {
      protected B buildable;
      @Override
      public L setValue(Object value)
      {
           //set the value
           return this;//&lt;-- returns Object, blocking the use of the ConcreteBuilder methods
      }
      public abstract B build();
  }
  </code></pre>
  
  <p>| </p>
  
  <pre><code>public class ConcreteBuilder extends AbstractBuilder&lt;ConcreteProduct, ConcreteBuilder, ConcreteValueSetter&gt; implements ConcreteValueSetter
  {
      @Override
      public ConcreteBuilder setConcreteValue(int num)
      {
          //set value
          return this;
      }
      @Override
      public ConcreteProduct build();
      {
          return buildable;
      }
  }
  </code></pre>
  
  <p>|</p>
  
  <pre><code>public interface ValueSetter&lt;L&gt;
  {
       public L setValue(Object value);
  }
  </code></pre>
  
  <p>|</p>
  
  <pre><code>public interface ConcreteValueSetter
  {
      public ConcreteBuilder setConcreteValue(int num);
  }
  </code></pre>
  
  <p>As marked, this stops the chain when 'switching' to the subclass build methods.
  I have made some variants to this and I cannot get it to work.</p>
  
  <p>So I am really wondering if this is even possible. If it is, I would like to see how. If it isn't I would like know some technique that does meet my requirements.</p>
  
  <p>Thanks in advance!</p>