title: Java Generics: Multiple Inheritance in Bounded Type Parameters <T extends A & I>
body: <p>I am about to create a factory which creates objects of a certain type T which extends a certain class A and another interface I. However, T must not be known. Here are the minimum declarations:</p>
  
  <pre><code>public class A { }
  public interface I { }
  </code></pre>
  
  <p>This is the factory method:</p>
  
  <pre><code>public class F {
      public static &lt;T extends A &amp; I&gt; T newThing() { /*...*/ }
  }
  </code></pre>
  
  <p>This compiles all fine.</p>
  
  <p>When I try to use the method the following works fine:</p>
  
  <pre><code>A $a = F.newThing();
  </code></pre>
  
  <p>...while this does not:</p>
  
  <pre><code>I $i = F.newThing();
  </code></pre>
  
  <p>The compiler complains:</p>
  
  <blockquote>
    <p>Bound mismatch: The generic method newThing() of type F is not applicable for the arguments (). The inferred type I&amp;A is not a valid substitute for the bounded parameter </p>
  </blockquote>
  
  <p>I can't understand why. It is clearly stated that "newThing returns something of a certain type T which does extend the class A and implement the interface I". When assigning to A everything works (since T extends A) but assigning to I does not (because of <em>what?</em>, clearly the thing returned <em>is</em> both an A <em>and</em> an I)</p>
  
  <p>Also: When returning an object, say B of the type <code>class B extends A implements I</code>, I need to cast it to the return type T, although B matches the bounds:</p>
  
  <pre><code>&lt;T extends A &amp; I&gt; T newThing() {
      return (T) new B();
  }
  </code></pre>
  
  <p>However, the compiler does not throw any warnings like UncheckedCast or the like.</p>
  
  <p>Thus my question:</p>
  
  <ul>
  <li>What is going wrong here?</li>
  <li>Is there an easy away to achieve the desired behavior (i.e. assigning to a variable of static type A or I), like there is in solving the return-type-problem by casting, in the factory method?</li>
  <li><strong>Why does the assignment to A work, while to I does not?</strong></li>
  </ul>
  
  <p>--</p>
  
  <p><strong>EDIT:</strong> Here the complete code snippet which totally works using Eclipse 3.7, project set up for JDK 6:</p>
  
  <pre><code>public class F {
      public static class A { }
      public static interface I { }
  
      private static class B extends A implements I {  }
  
      public static &lt;T extends A &amp; I&gt; T newThing() {
          return (T) new B();
  }
  
      public static void main(String... _) {
          A $a = F.newThing();
          // I $i = F.newThing();
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> Here is a complete example with methods and invocation which <em>does work at runtime</em>:</p>
  
  <pre><code>public class F {
      public static class A {
          int methodA() {
              return 7;
          }
      }
      public static interface I {
          int methodI();
      }
  
      private static class B extends A implements I {
          public int methodI() {
              return 12;
          }
      }
  
      public static &lt;T extends A &amp; I&gt; T newThing() {
          return (T) new B();
      }
  
      public static void main(String... _) {
          A $a = F.newThing();
          // I $i = F.newThing();
          System.out.println($a.methodA());
      }
  }
  </code></pre>