title: Understanding upper and lower bounds on ? in Java Generics
body: <p>I am really having a tough time understanding the wild card parameter. I have a few questions regarding that.  </p>
  
  <ol>
  <li><p><code>?</code> as a type parameter can only be used in methods. eg: <code>printAll(MyList&lt;? extends Serializable&gt;)</code> I cannot define classes with <code>?</code> as type parameter.  </p></li>
  <li><p>I understand the upper bound on <code>?</code>.  <code>printAll(MyList&lt;? extends Serializable&gt;)</code> means: "<em><code>printAll</code> will print <code>MyList</code> if it has objects that implement the <code>Serialzable</code> interface.</em>"<br>
  I have a bit of an issue with the <code>super</code>. <code>printAll(MyList&lt;? super MyClass&gt;)</code> means: "<em><code>printAll</code> will print <code>MyList</code> if it has objects of <code>MyClass</code> or any class which extends <code>MyClass</code> (the descendants of <code>MyClass</code>).</em>"  </p></li>
  </ol>
  
  <p>Correct me where I went wrong.  </p>
  
  <p><em>In short, only <code>T</code> or <code>E</code> or <code>K</code> or <code>V</code> or <code>N</code> can be used as type parameters for defining generic classes. <code>?</code> can only be used in methods</em><br>
  <hr>
  <strong>Update 1:</strong>  </p>
  
  <pre><code>public void printAll(MyList&lt;? super MyClass&gt;){
      // code code code
  }
  </code></pre>
  
  <p>Accordint to Ivor Horton's book, <code>MyList&lt;? super MyClass&gt;</code> means that I can print <code>MyList</code> if it has objects of <code>MyClass</code> or any of the interfaces or classes it implements. That is, <code>MyClass</code> is a <strong>lower bound</strong>. It is the last class in the inheritance hierarchy. This means my initial assumption was wrong.</p>
  
  <p>So, say if <code>MyClass</code> looks like:  </p>
  
  <pre><code>public class MyClass extends Thread implements ActionListener{
      // whatever
  }
  </code></pre>
  
  <p>then, <code>printAll()</code> will print if<br>
  1. There are objects of <code>MyClass</code> in the list<br>
  2. There are objects of <code>Thread</code> or <code>ActionListener</code> in the <code>List</code><br>
  <hr>
  <strong>Update 2:</strong>  </p>
  
  <p>So, after having read the many answers to the question, here is my understanding:  </p>
  
  <ol>
  <li><p><code>? extends T</code> means <strong>any class which extends <code>T</code></strong>. Thus, we are referring to the <em>children of <code>T</code></em>. Hence, <strong><code>T</code> is the upper bound. The upper-most class in the inheritance hierarchy</strong>  </p></li>
  <li><p><code>? super T</code> means <strong>any class / interface which is <code>super</code> of <code>T</code></strong>. Thus we are referring to all the <em>parents of <code>T</code></em>. <strong><code>T</code> is thus the lower bound. The lower-most class in the inheritance hierarchy</strong></p></li>
  </ol>