title: How to have Java Iterable return base subtype from extended generic
body: <p>I am attempting to make a class that sub-classes Iterable.  This is similar to this <a href="https://stackoverflow.com/questions/21512250/how-can-i-make-my-class-iterable-so-i-can-use-foreach-syntax">question</a>. However, I need to have the objects in the list be sub-classes of a specific class, e.g. QObject in my example below. In addition, I need my iterator to implement specific methods related to the ListIterator interface.</p>
  
  <p>The problem is that my inner class that implements the ListIterator interface does not "return" the base generic type QObject in my implementation.  There is something wrong with how I am doing the QueueListIterator generics.</p>
  
  <p>Now, if I declare the Queue in my example code with Queue &lt;QObject&gt; it works.  This must be providing the information needed to allow the iterator to get the type correct.  However, just declaring it as Queue does not, even though the base generic type is QObject AND my other methods in the Queue class use the base generic type.</p>
  
  <p>So, my question is why does this happen and is there a way to specify QueueListIterator to discern the base generic type?</p>
  
  <pre><code>    public class Queue&lt;T extends QObject&gt; implements Iterable&lt;T&gt; {
          /**
           * The list of items in the queue.
           */
          protected List&lt;T&gt; myList;
  
          // other methods and defs
  
          public final Iterator&lt;T&gt; iterator() {
              return (new QueueListIterator());
          }
  
          public final ListIterator&lt;T&gt; listIterator() {
              return (new QueueListIterator());
          }
  
          private class QueueListIterator implements ListIterator&lt;T&gt; {
  
              protected ListIterator&lt;T&gt; myIterator;
  
              protected QueueListIterator() {
                  myIterator = myList.listIterator();
              }
  
              @Override
              public void add(T o) {
                  throw new UnsupportedOperationException("The method add() is not supported for Queue iteration");
              }
  
              @Override
              public boolean hasNext() {
                  return myIterator.hasNext();
              }
  
              @Override
              public boolean hasPrevious() {
                  return myIterator.hasPrevious();
              }
  
              @Override
              public T next() {
                  return myIterator.next();
              }
  
              @Override
              public int nextIndex() {
                  return myIterator.nextIndex();
              }
  
              @Override
              public T previous() {
                  return myIterator.previous();
              }
  
              @Override
              public int previousIndex() {
                  return myIterator.previousIndex();
              }
  
              @Override
              public void remove() {
                  throw new UnsupportedOperationException("The method remove() is not supported for Queue iteration");
              }
  
              @Override
              public void set(T o) {
                  throw new UnsupportedOperationException("The method set() is not supported for Queue iteration");
              }
          }
      }
  
      // testing code
      public static void test(){
  
              Queue q = new Queue();
              // put some stuff in the Queue
              QObject r1 = new QObject(q.getTime(), "A");
              q.enqueue(r1);
              QObject r2 = new QObject(q.getTime(), "B");
              q.enqueue(r2);
  
              // Does not work! 
              // incompatible object types, Object cannot be converted to QObject in the for( : ) construct
              for (QObject qo : q) {
                  System.out.println(qo);
              }
      }
  </code></pre>