I've noticed something funny Java does (or at least Netbeans) when I use classes implementing ArrayList
and changing the generics type of the elements. I basically created an abstract class that extends ArrayList
and some subclasses that are supposed to work with String
objects (so something like ArrayList<String>
). One of the things I did to try to achieve that was this:
public abstract class A extends ArrayList {
...
}
@Override
public abstract class B extends A {
public Iterator<String> iterator() {
return super.iterator();
}
}
Another one was this:
public abstract class A extends ArrayList {
...
}
public abstract class B<String> extends A {
@Override
public Iterator<String> iterator() {
return super.iterator();
}
}
The first one overrides successfully the iterator()
method assigning a String value to it. The other one somehow cancels out the type casting. The funny thing is that none of them works when it comes to for loops. This receives type Object
instead of String
.
for (String s : B) {
...
}
Do you have any idea why this happens and how can I fix it without implementing my own iterator?