title: Java: Convert between iterator types (interface / implementing class)?
body: <p>In a class, I have a container:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class MyClass implements MyClassInterface {
    private LinkedList&lt;OtherClass&gt; list; //Need to use 'OtherClass' instead of 'OtherClassInterface here
    @Override
    public Iterator iterator() {
      return list.iterator; //Problem!
    }
  }
  </code></pre>
  
  <p>The interface:</p>
  
  <pre class="lang-java prettyprint-override"><code>public interface MyClassInterface {
    //Is
    public Iterator iterator();
    //Should be
    public Iterator&lt;OtherClassInterface&gt;();
  }
  </code></pre>
  
  <p>Then again, <code>OtherClass</code> also has an interface <code>OtherClassInterface</code>.
  I want only the interfaces to be used by whom who works with the code.
  The problem is that I want to use the full <code>OtherClass</code> inside <code>MyClass</code> but pass an iterator over <code>LinkedList&lt;OtherClassInterface&gt;</code> to the caller of <code>MyClassInterface.iterator()</code>.</p>
  
  <p>I could not cast the existing <code>LinkedList&lt;OtherClass&gt;</code> to <code>LinkedList&lt;OtherClassInterface&gt;</code> inside <code>MyClass</code> to return the desired iterator.</p>
  
  <p>How to handle such a situation?</p>
  
  <h1>EDIT</h1>
  
  <h2>Reason why I want this behaviour</h2>
  
  <p>For another developer, I want to provide two interfaces: The first gives him access to a higher data structure which contains a lower data structure which he should access by the second interface. In the implementing class of the higher interface I use the type of the lower data structure directly, not over the lower interface.</p>
  
  <p>As mentioned, the other developer wants to use both interfaces. Over the higher one I want to provide an iterator that gives access to elements of the lower interface - but not to the class that implements the interface.</p>
  
  <h2>Additional needs</h2>
  
  <p>I also want the returned iterator to be "Iterable" i.e. so that I can use the "for each" construct. Is this also possible with *waxwing*s solution? If possible, I wouldn´t like to implement an own iterator - for me this seems not neccessary because I just want to give an iterator over elements of the interface instead of the implementing class.</p>