i have a problem that stunned me a lot.
I have a class (Bar) with a getter method which returns a List:
public abstract class Bar<T extends IdentifiableObject> {
...
public List<Bar> getBars() {
return bars;
}
...
The hierarchy i have is:
public abstract class Bar<T extends IdentifiableObject>;
public abstract class IdentifiableObject<T extends Comparable<T>>;
Until here everything works fine, the problem is when I execute:
for(Bar bar : bars.getBars()) {
}
The previous sentence throws a compilation error:
incompatible types: java.lang.Object cannot be converted to java.util.List<com.x.Bar>
I was suprised with this error but i was more suprised when i performed
List<Bar> bars = bar.getBars();
Iterator<Bar> iterator = bar.getBars().iterator();
both without problems
What is wrong here in the "for" sentence?
Thank you