Java binary tree which return type for iterator over nodes, interface or class?
<p>I'm writing a class for a binary tree, and in the tree interface this function is defined:</p>

<pre><code>/** Returns an iterable collection of the the nodes. */
    public Iterable&lt;Position&lt;E&gt;&gt; positions();
</code></pre>

<p>The problem is that the type of the node is MY class implementing the position interface in the task. Not the interface itself, Position. That is why I'm having trouble returning an iterable list of the nodes. </p>

<pre><code>@Override
    public Iterable&lt;Position&lt;E&gt;&gt; positions() {
     ArrayList&lt;Posisjon&lt;E&gt;&gt; liste = new ArrayList&lt;Posisjon&lt;E&gt;&gt;();
     liste = dumpings(liste,root);
     System.out.println(liste.get(0));
     return (Iterable&lt;Position&lt;E&gt;&gt;) liste.Iterator(); //PROBLEM HERE!
    }
</code></pre>

<p>I use a recursive helper function to extract the elements and add them to an ArrayList and then just return the iterator for the list. "liste" here is of type my class "Posisjon" and the expected return for the function is the interface "Position". Why can't I just use return type "Posisjon" since it is implementing interface "Position"? Nor can I change the return type because the return type is specified in the tree interface which I must implement. </p>

<p>Here is the interface "Position" and my class "Posisjon" in case it helps you to understand the problem. (Node interface and Node class if you will).</p>

<pre><code>public interface Position&lt;E&gt; {
  /** Return the element stored at this position. */
  E element();
}
</code></pre>

<p><hr /></p>

<pre><code>public class Posisjon&lt;E&gt; implements Position&lt;E&gt; {
    private E element;
    private Posisjon&lt;E&gt; parenten;
    private Posisjon&lt;E&gt; rightChildren;
    private Posisjon&lt;E&gt; leftChildren;
    @Override
    public E element() {
     return element;
    }
    public E setElement(E ting){
     E tmpElement = element;
     this.element = ting;
     return tmpElement;
    }
    public Posisjon&lt;E&gt; leftChild(){
     return leftChildren;
    }
    public Posisjon&lt;E&gt; rightChild(){
     return rightChildren;
    }
    public Posisjon&lt;E&gt; parent(){
     return parenten;
    }
    public Posisjon(E element){
     this.element = element; 
    }
    public void setLeftChild(Posisjon&lt;E&gt; ting){
     this.leftChildren = ting; 
    }
    public void setRightChild(Posisjon&lt;E&gt; ting){
     this.rightChildren = ting; 
    }
}
</code></pre>
