I have a class B which extends A. A stores a list of AItem and B stores a list of BItem
In 'A' I have an ArrayList which uses [? extends AItem] .
I assume this means that I can use this ArrayList for objects of any type that extends AItem.
So in my B class I have a method add(..) which adds a BItem to the items.
I assumed this would work because the items arrayList can hold a list of any object that extends from AItem.
import java.util.ArrayList;
class A {
public ArrayList<? extends AItem> items;
}
public class B extends A{
public void add(BItem i) {
this.items.add(i); //compile error here
}
}
//items :
class AItem {}
class BItem extends AItem{}
How would I get this to work?
My compile error looks like this :
The method add(capture#2-of ? extends AItem) in the type ArrayList is not applicable for the arguments (BItem)