You cannot accomplish what you're trying to do. You must wonder why you can't add things of type Concept
to your List<? extends Concept>
. Here is why:
Consider this case:
List<? extends Concept> list = new List<Link>();
And that you have another class that also extends Concept
besides Link
, let's call it Link2
:
public class Link2 extends Concept { ... }
Then here comes the problem. What if you do:
Link2 myLink2 = new Link2();
list.add(myLink2);// Big problem, remember that your list was instantiated as a list of Links not Link2s.
So that's why you cannot add items to a list of wildcard types like that, because you might try to add something to it that will simply not work with the list type it was instantiated as.
Now, my suggestion for what you're trying to do. Make your base class generic and then your extending classes only have to specify the concrete type it will handle. Something like this:
public class Concepts<T extends Concept> {
private List<T> list = new List<T>();
public void add(T test) {
list.add(test);
}
}
public class Links extends Concepts<Link> {
}