title: How to handle polymorphism with interfaces in bounded type parameters? (Java)
body: <p>I've got a class hierarchy of the following kind:</p>
  <pre><code>class Declarations {
  
      interface Function&lt;I, O&gt; {
          interface BinaryFunction&lt;I&gt; extends Function&lt;I, Boolean&gt; {}
      }
  
      static abstract class FunctionImpl&lt;I, O&gt; implements Function&lt;I, O&gt; {
  
          static class BinaryFunctionImpl&lt;I&gt;
                 extends FunctionImpl&lt;I, Boolean&gt;
                 implements Function.BinaryFunction&lt;I&gt; {}
      }
  
      interface Optimizer&lt;I, O, F extends Function&lt;I, O&gt;&gt; {
  
          interface BinaryOptimizer&lt;I&gt;
          extends Optimizer&lt;I, Boolean, Function.BinaryFunction&lt;I&gt;&gt; {}
      }
  
      static abstract class AbstractOptimizer&lt;I, O, F extends Function&lt;I, O&gt;&gt;
                      implements Optimizer&lt;I, O, F&gt; {}
  
      static abstract class OptimizerImpl&lt;I, O, F extends FunctionImpl&lt;I, O&gt;&gt;
                      extends AbstractOptimizer&lt;I, O, F&gt; {
  
          static class BinaryOptimizerImpl&lt;I&gt;
                 extends OptimizerImpl&lt;I, Boolean, FunctionImpl.BinaryFunctionImpl&lt;I&gt;&gt;
                 implements BinaryOptimizer&lt;I&gt; {}
      }
  }
  </code></pre>
  <p>The compiler stumbles upon the declaration of <code>BinaryOptimizerImpl&lt;I&gt;</code> and says:</p>
  <blockquote>
  <p>'<code>Optimizer</code>' cannot be inherited with different type arguments:
  '<code>BinaryFunctionImpl&lt;I&gt;</code>' and '<code>BinaryFunction&lt;I&gt;</code>'</p>
  </blockquote>
  <p>But I don't understand the issue, as <code>BinaryFunctionImpl&lt;I&gt;</code> implements <code>BinaryFunction&lt;I&gt;</code> and both are passed the same parameter. So these two types should be compatible from my understanding. If <code>BinaryFunction&lt;I&gt;</code> was a class rather than an interface, I'd understand that constrained type parameters don't allow for instantiating them with subclasses unless declared with <code>? extends Type</code>. But for interfaces, this wouldn't make sense, as you can never instantiate an interface directly.</p>
  <p>All of the shown types are already in active use within the surrounding codebase, except for <code>OptimizerImpl</code>/<code>BinaryOptimizerImpl</code>, which must provide the respective <code>Optimizer</code> interface. Also note that the inheritance from <code>AbstractOptimizer</code> in <code>OptimizerImpl</code> cannot be resolved by integrating it into <code>OptimizerImpl</code>, as it is also inherited from elsewhere (I know, the naming isn't ideal, sorry!).</p>
  <p>How to make this compile with minimal impact on the existing class hierarchy, i.e. changing only the lowest-possible (most specific) type layer, to reduce the impact on the existing codebase? Is there a way to make this compile by only changing the declaration of <code>BinaryOptimizerImpl</code>? (Leaving away the obvious non-solution of not making it implement <code>BinaryOptimizer</code>)</p>
  <p>I hope my question is comprehensible, I had to abstract a lot. I've done quite some research on the semantics of Generics in Java, but there's a large variety of mistakes to be made and I couldn't find any explanation for this one.</p>