Why isn't this common interface working?

I have a set of generic interfaces and classes

public interface IElement {
// omited
}

class Element implements IElement {
// omited
}

public interface IElementList<E extends IElement>  extends Iterable {
   public Iterator<E> iterator();
}

class ElementList implements IElementList<Element> {

    public Iterator<Element> iterator() {
       // omited
       }
}


public interface IElementListGroup<E extends IElementList<? extends IElement>> {
    public E getChosenElementList();
}


class ElementListGroup implements IElementListGroup<ElementList> {
    public ElementList getChosenElementList() {
        // omited
    }
}

      

And then the simple code

ElementListGroup group;

for(Element e : group.getChosenElementList())
{
 // omited
}

      

And the line with the throwe keyword is an error "don't convert from Object to Element".

Thanks in advance.

0


source to share


2 answers


IElementList

need to be implemented Iterable<E>

. Otherwise, the interface points Iterator iterator()

rather than Iterator<E> iterator()

. This makes the compiler think you are repeating Object

s.



I made this change and compiled it (after adding some null returns).

+7


source


Your function is returning an ElementList, not an Element, and ElementList is not an iterable Element



0


source







All Articles