I've implemented Iterable<> in my base class but the subclass will not allow an implicit forloop.
Why is the Iterator<> working correctly but the for-loop complaining of
Can only iterate over an array or an instance of java.lang.Iterable
I would have expected the Iterable<> interface to be visible in the subclass. What am I missing?
package tester;
import java.util.ArrayList;
import java.util.Iterator;
public class TestInher {
private Concrete<Integer> mList = new Concrete<Integer>();
public TestInher() {}
public abstract class Base<T>
implements Iterable<T>
{
protected ArrayList<T> list = new ArrayList<T>();
@Override
public Iterator<T> iterator() {return list.iterator();}
public abstract void add(T item);
}
public abstract class ClassAbstract<T> extends Base<T>{}
public class Concrete<T>
extends ClassAbstract<T>
{
@Override
public void add(T item) {list.add(item);}
}
public void doIt() {
for(int i=0; i<10; i++) {mList.add(i);}
Iterator<Integer> i = mList.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
//Can only iterate over an array or an instance of java.lang.Iterable
for(Integer i : mList.iterator()) {
}
}
}