title: Confusing explanation of limiting the type parameter of a generic binary search tree in Java
body: <p>I'm new to generics (and Java, and Stack Overflow), and there is a point in the textbook I'm learning from where it discusses the implementation of a generic binary search tree (excerpt below).</p>
  
  <blockquote>
    <p>The Comparable interface is generic, so let’s consider storing the following
    type of elements in our search tree:</p>
    
    <p>&lt; T extends Comparable&lt; T>></p>
    
    <p>This still causes a problem. Suppose that both Dog and Cat are subclasses of a
    class Mammal and that the Mammal class implements the Comparable interface.
    Now if we create a binary search tree that stores Mammal objects, then a
    Dog and Cat could be added, but are not really comparable to each other. So this
    solution, if used in this particular way, has the same problem as using the nongeneric
    version of Comparable.
    A more comprehensive solution, though not as intellectually satisfying, is to
    write the generic type as:</p>
    
    <p>&lt; T extends Comparable&lt; ? super T>></p>
    
    <p>This declaration restricts the comparable nature of the element to any superclass
    of T.</p>
  </blockquote>
  
  <p>So I get why the type parameter needs to be <code>Comparable&lt; T&gt;</code>, but then the book claims that this can pose problems if the tree of <code>Mammal</code>s contains different subtypes that attempt to invoke the <code>compareTo()</code> method on each other. Well if the reference type for the <code>Cat</code> and <code>Dog</code> objects in the tree is <code>Mammal</code>, then wouldn't they be invoking <code>Mammal</code>'s <code>compareTo()</code> method anyway (being <code>Comparable&lt; Mammal&gt;</code>), making it a valid comparison (just on the <code>Mammal</code> level)?</p>
  
  <p>I don't understand what difference <code>Comparable&lt; ? super T&gt;</code> makes, because would that not be the same thing, except perhaps if <code>Mammal</code>s aren't <code>Comparable</code> then it falls back to a <code>Comparable</code> superclass like the <code>Animal</code> class or something?</p>
  
  <p>I'm probably missing something, like some nasty consquence of type erasure or something that will cause the comparisons to not work like I'm thinking they would.</p>