title: Java: Declaring variables with multiple generic "constraints" on them
body: <p>I have a bunch of classes in java that all implement an interface called IdObject (specifying a getId() method). Moreover, they also all implement Comparable&lt;> with themselves as type parameter, so they are all comparable to themselves.</p>
  
  <p>What I'd like to do is declare a list of such objects, fill it, then sort it and call getId() on them. So my code looks like this:</p>
  
  <pre><code>List&lt;? extends IdObject &amp; Comparable&lt;?&gt;&gt; objectList = null;
  
  if (foo) {
      objectList = new ArrayList&lt;TypeA&gt;();
      ...
  } else if (bar) {
      objectList = new ArrayList&lt;TypeB&gt;();
      ...   
  }
  
  if (objectList != null) {
      Collections.sort(objectList);
      for (IdObject o : objectList) {
          System.out.println(o.getId());
      }
  }
  </code></pre>
  
  <p>Basically my problem lies in the first line -- I want to specify two "constraints" for the type because I need the first one to make sure I can print the ID in the loop and the second one to make sure I can use Collections.sort() on the list.</p>
  
  <p>The first line does not compile.</p>
  
  <p>Is there a way to make this work without specifying a generic type without type parameters and using unchecked operations? I could also not find an example of this on the internet.</p>
  
  <p>Greetings</p>