title: Java Generics of the form <T extends A<T>> and Inheritance
body: <p>I have a tricky problem with Java generics, or maybe I can't see the wood for the trees.</p>
  
  <p>I have three classes, A, B, and C that look as follows.</p>
  
  <pre><code>abstract class A&lt;T extends A&lt;T&gt;&gt; {
      abstract T sefl();
  };
  
  abstract class B&lt;T extends B&lt;T&gt;&gt; extends A&lt;T&gt; {
  };
  
  class C extends B&lt;C&gt; {
      @Override
      C sefl() {
          return this;
      }
  }
  </code></pre>
  
  <p>Later I have different versions of B's as well as different versions of C's. Furthermore, I have a function test that should accept a list of B's (or of one of its cousins). Or in general, it should accept any list of elements that inherit from A. Unfortunately, I need to know the type of the list's elements in the function's body, i.e. the uppermost type a.self can return (or the type T). Function test looks as follows:</p>
  
  <pre><code>static &lt;T extends A&lt;T&gt;&gt; void test(List&lt;T&gt; list) {
  
      for (A&lt;T&gt; a : list) {
          @SuppressWarnings("unused")
          T t = a.sefl();
      }
  
  }
  </code></pre>
  
  <p>Now, calling test with a list of C's works.</p>
  
  <pre><code>List&lt;C&gt; cs = new LinkedList&lt;C&gt;();
  test(cs);
  </code></pre>
  
  <p>But</p>
  
  <pre><code>List&lt;B&gt; bs = new LinkedList&lt;B&gt;();
  test(bs);
  </code></pre>
  
  <p>results in a warning requiring a type parameter and</p>
  
  <pre><code>List&lt;B&lt;?&gt;&gt; bs = new LinkedList&lt;B&lt;?&gt;&gt;();
  test(bs);
  </code></pre>
  
  <p>is not valid. Where is my mistake, or how can I create a list of B's that is accepted by function test?</p>
  
  <hr>
  
  <p>Some words to the motivation behind this problem. The classes A, B, and C (or Animal, Mammal, and Cat) implement a tree-like data structure, where each class extends the structure with some properties. Typically, all super-classes are abstract and you can only create instances from leaf-classes, e.g. cat. Now, the difficulty is that the classes implement a copy-on-write policy (COW), i.e. modifying an object creates and returns a new instance of itself with the modified property.</p>
  
  <p>For example, let’s say all animals have an age property. You can easily define this property in Animal, and you can provide a getter method to return the age.</p>
  
  <pre><code>abstract class Animal&lt;T extends Animal&lt;T&gt;&gt; {
      private int age;
      public int getAge(int age) {
          return age;
      }
  };
  </code></pre>
  
  <p>However, how do you define the setter method? You can write it as follows:</p>
  
  <pre><code>public abstract Animal setAge();
  </code></pre>
  
  <p>This requires that (at least) each non-abstract element must implement the setter function. For example:</p>
  
  <pre><code>class Cat extends Mammal&lt;C&gt; {
  
      @Override
      public Animal setAge(int age) {
          return new Cat(/* .. */);
      }
  }
  </code></pre>
  
  <p>Remember, as we implement a COW policy, we must create a new instance. So, in the setter function (e.g. implement in Cat) we return a new cat with the new age. Calling cat.setAge(4) on a Cat element returns a new Cat. Unfortunately, because of the type signature, we only now that we got an Animal returned from setAge, even if we call it on a Cat directly. The twist with the generics helps to reveal the concrete type when calling setAge. So, we can construct Animal like this:</p>
  
  <pre><code>abstract class Animal&lt;T extends Animal&lt;T&gt;&gt; {
      private int age;
      public int getAge(int age) {
          return age;
      }
      public abstract T setAge();
  };
  </code></pre>
  
  <p>And in Cat we can say:</p>
  
  <pre><code>class Cat extends Mammal&lt;C&gt; {
  
      @Override
      public Cat setAge(int age) {
          return new Cat(/* .. */);
      }
  }
  </code></pre>
  
  <hr>
  
  <p>So, back to the problem. Your right, using <code>List&lt;? extends Animal&lt;?&gt;&gt;</code> as the type of the list works, but unfortunately, I need some way to know the type of the elements. Or more concrete: Function test must replace the old element with the new one. For example:</p>
  
  <pre><code>static void test2(List&lt;? extends Animal&lt;?&gt;&gt; list) {
      for (Animal&lt;?&gt; animal : list) {
          @SuppressWarnings("unused")
          Animal&lt;?&gt; a = animal.setAge(4711);
          list.add(a);
      }
  }
  </code></pre>
  
  <p>And unfortunately, the list extension list.add(a); ist the statement that doesn't work with this signature.</p>