tags:

views:

140

answers:

2

I have a bunch 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 a simple code

ElementListGroup group;

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

And the line with for keyword throwe a "cannot convert from element type Object to Element" compiler error.

Thanks in advance.

+7  A: 

IElementList needs to implement Iterable<E>. Otherwise, the interface specifies Iterator iterator(), not Iterator<E> iterator(). This makes the compiler think that you're iterating over Objects.

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

Michael Myers
I'm surprised that the compiler didn't bite the OP's head off over using a raw type. Or perhaps the OP has a habit of ignoring warnings, in which case they deserve what they get. :-P
Chris Jester-Young
There's more rawtype warnings in the JDK7 javac.
Tom Hawtin - tackline
A: 

Your function returns an ElementList not an Element, and ElementList is not an iterable over Element

Douglas Mayle