title: How to make a Java class that implements one interface with two generic types?
body: <p>I have a generic interface</p>
  
  <pre><code>public interface Consumer&lt;E&gt; {
      public void consume(E e);
  }
  </code></pre>
  
  <p>I have a class that consumes two types of objects, so I would like to do something like:</p>
  
  <pre><code>public class TwoTypesConsumer implements Consumer&lt;Tomato&gt;, Consumer&lt;Apple&gt;
  {
     public void consume(Tomato t) {  .....  }
     public void consume(Apple a) { ...... }
  }
  </code></pre>
  
  <p>Apparently I can't do that.</p>
  
  <p>I can of course implement the dispatch myself, e.g.</p>
  
  <pre><code>public class TwoTypesConsumer implements Consumer&lt;Object&gt; {
     public void consume(Object o) {
        if (o instanceof Tomato) { ..... }
        else if (o instanceof Apple) { ..... }
        else { throw new IllegalArgumentException(...) }
     }
  }
  </code></pre>
  
  <p>But I am looking for the compile-time type-checking and dispatching solution that generics provide.</p>
  
  <p>The best solution I can think of is to define separate interfaces, e.g.</p>
  
  <pre><code>public interface AppleConsumer {
     public void consume(Apple a);
  }
  </code></pre>
  
  <p>Functionally, this solution is OK, I think. It's just verbose and ugly.</p>
  
  <p>Any ideas? </p>