title: How do I resolve ambiguous methods caused by intersection types in Java generics?
body: <p>I just recently discovered that you can specify multiple types in a single type parameter bound (see example). Like any new tool, I've been trying to explore the possibilities of how this can be used (and misused). I crafted this example to help illustrate. </p>
  
  <p>On the sample below, the compiler is giving me an error</p>
  
  <blockquote>
    <p><em>dispatch(new AlphabetSoup());</em></p>
    
    <p><strong>The method dispatch(Demo.Soup) is ambiguous for the type Demo</strong></p>
  </blockquote>
  
  <p>I can understand this because either method signature matches. My question is how could this be resolved without changing the methods? If I wanted force a call to the Soup version I could downcast to Soup: </p>
  
  <blockquote>
    <p>dispatch((Soup) new AlphabetSoup()) </p>
  </blockquote>
  
  <p>But I'm unsure how you'd force a call to the other version. Is it possible?</p>
  
  <pre><code>public class Demo {
  
      interface HasA { public char getA(); }
      interface HasB { public char getB(); }
      interface HasC { public char getC(); }
  
      interface Soup { 
          public void eat();
      }
  
      class Alphabet implements HasA, HasB, HasC {
          public char getA() { return 'a'; }
          public char getB() { return 'b'; }
          public char getC() { return 'c'; }
      }
  
      class AlphabetSoup implements Soup,  HasA, HasB, HasC  { 
          public void eat() { System.out.println("Mmm Mmm Good!"); }
          public char getA() { return 'a'; }
          public char getB() { return 'b'; }
          public char getC() { return 'c'; }
      }
  
      public void dispatch(Soup soup) {
          System.out.println("Eating some soup...");
          soup.eat();
      }
  
      public &lt;T extends HasA &amp; HasB &amp; HasC&gt; void dispatch(T letters) {
          System.out.println("Reciting ABCs...");
          System.out.println(letters.getA());
          System.out.println(letters.getB());
          System.out.println(letters.getC());
      }
  
      public void test() {
          dispatch(new Alphabet());
          dispatch(new AlphabetSoup());
      }
  
  
      public static void main(String[] args) {
          new Demo().test();
      }
  }
  </code></pre>
  
  <p>--
  Edit: Just learned that "multiple bounded type parameters are formally referred to as "Intersection Types"</p>