title: In Java, how do we make a type parameter of a generic to take a type parameter itself?
body: <p>In Java, how do we make a type parameter of a generic to take a type parameter itself? What I would really like to do is the following:</p>
  
  <pre><code>public class TransformerBase&lt;T, TRANSFORMER&lt;U&gt; extends TransformerBase&lt;U, TRANSFORMER&gt;&gt;
  {
  
    public &lt;O&gt; TRANSFORMER&lt;O&gt; transform1(TransformFunction&lt;T, O&gt; f)
    {
      ...
    }
  }
  </code></pre>
  
  <p>And one would extend the TransformerBase class and add their own transform methods, while retaining the original transform methods in the base class, like this:</p>
  
  <pre><code>public class MyTransformer&lt;T&gt; extends TransformerBase&lt;T, MyTransformer&gt;&gt;
  {
  
    public &lt;O&gt; MyTransformer&lt;O&gt; transform2(TransformFunction&lt;T, O&gt; f)
    {
      ...
    }
  }
  </code></pre>
  
  <p>Then the user of the class can chain the method calls like this:</p>
  
  <pre><code>MyTransformer&lt;String&gt; input = new MyTransformer&lt;&gt;();
  MyTransformer&lt;String&gt; result = input
    .transform1(new TransformFunction&lt;String, Integer&gt;() {...})
    .transform2(new TransformFunction&lt;Integer, String&gt;() {...});
  </code></pre>
  
  <p>This is similar to the problem outlined <a href="https://stackoverflow.com/questions/17164375/subclassing-a-java-builder-class">here</a> but with the addition that the "Builder" itself takes a type parameter.</p>
  
  <p><strong>But the thing is, it does not look like Java supports this kind of construct. Is there another way to solve this problem?</strong></p>
  
  <p>I tried declaring the base class like this:</p>
  
  <pre><code>public class TransformerBase&lt;T, TRANSFORMER extends TransformerBase&lt;T, TRANSFORMER&gt;&gt;
  {
  
    public &lt;O, TFM_OUT extends TransformerBase&lt;O, TFM_OUT&gt;&gt; TFM_OUT transform1(TransformFunction&lt;T, O&gt; f)
    {
      ...
    }
  }
  </code></pre>
  
  <p>And the extended class:</p>
  
  <pre><code>public class MyTransformer&lt;T&gt; extends TransformerBase&lt;T, MyTransformer&lt;T&gt;&gt;
  {
    public &lt;O, TFM_OUT extends MyTransformer&lt;O&gt;&gt; TFM_OUT transform2(TransformFunction&lt;T, O&gt; f)
    {
      ...
    }
  }
  </code></pre>
  
  <p>The user can do this:</p>
  
  <pre><code>MyTransformer&lt;Integer&gt; input = new MyTransformer&lt;&gt;();
  
  MyTransformer&lt;String&gt; t = input.transform1(new TransformFunction&lt;Integer, String&gt;(){...});
  
  MyTransformer&lt;Integer&gt; result = t.transform2(new TransformFunction&lt;String, Integer&gt;(){...});
  </code></pre>
  
  <p>But in order for method chaining to work, the caller has to declare the type parameters explicitly, which kind of defeats the purpose of chaining:</p>
  
  <pre><code>MyTransformer&lt;String&gt; result = input
    .&lt;String, MyTransformer&lt;String&gt;&gt;transform1(new TransformFunction&lt;Integer, String&gt;(){ ... })
    .transform2(new TransformFunction&lt;String, Integer&gt;(){...});
  </code></pre>