I'm confused - and need help. I've an abstract class with an abstract method:
public abstract class AbstractA<T extends SomeOtherClass> {
public abstract List<String> getAll();
}
When I declare a method parameter of type AbstractA and call getAll() the IDE shows me the correct return type (List<String>
).
...
public void test(AbstractA abstractA) {
abstractA.getAll(); // <- returns List<String>
}
...
But when I want to iterate over that list, each element is of type Object and not String:
...
public void test(AbstractA abstractA) {
for(String element : abstractA.getAll()) { // <- compile error:
// error: incompatible types: Object cannot be converted to String
}
}
...
And it gets even better:
...
public void test(AbstractA abstractA) {
List<String> myElements = abstractA.getAll();
for(String element : myElements) { // <- this is correct!
}
}
...
I've no idea what's going on here. I'am using Java 8 (update 25). Can anyone explain to me why the compiler did not accept an iteration over my list?
I really appreciate your help.
Best regards, Daniel