title: Using 2 levels of generics in an Abstract Class definition
body: <p>I have this generic interface:</p>
  
  <pre><code>public interface TjbListener&lt;T&gt; {
      public void hearChange(T t);
  }
  </code></pre>
  
  <p>Which I use like this:</p>
  
  <pre><code>public interface ObjectOneListener extends TjbListener&lt;ClassOne&gt; {
  
  }
  </code></pre>
  
  <p>I would like to write an abstract generic class A which takes a generic type U as a parameter and has a method (1) which itself calls a method (2) on U. Below is my attempt  U should extend (or implement maybe?) the generic TjbListener interface.</p>
  
  <pre><code>public abstract class ListenerInformer&lt;U extends TjbListener&lt;"what should I write here"&gt;&gt; {
      List&lt;U&gt; mListeners = new ArrayList&lt;U&gt;();
  
      public void addListener(U u){
          mListeners.add(u);
      }
  
      public void informAll("what should I write here"){
          for(U u:mListeners){
              u.hearChange("what should I write here");
          }
      }
  }
  </code></pre>
  
  <p>One solution I thought of as I was writing this question is below, but I don't know if it's really a solution, or if it has subtle problems I don't understand:</p>
  
  <pre><code>public abstract class ListenerInformer&lt;U extends TjbListener&lt;T&gt;,T&gt; {
      List&lt;U&gt; mListeners = new ArrayList&lt;U&gt;();
  
      public void addListener(U u){
          mListeners.add(u);
      }
  
      public void informAll(T t){
          for(U u:mListeners){
              u.hearChange(t);
          }
      }
  }
  </code></pre>
  
  <p><strong>UPDATE: BEWARE</strong></p>
  
  <p>I have just discovered that this approach is almost useless for my particular case because the same class cannot implement the same interface with different parameters. See the question linked below. This means that I cannot have one class be a listener of two different types with my (or Johanna's) solution, without using a different strategy like composition.</p>
  
  <p><a href="https://stackoverflow.com/questions/1297972/how-to-make-a-java-class-that-implements-one-interface-with-two-generic-types">How to make a Java class that implements one interface with two generic types?</a></p>