I have got some error trying to use basic generics. Below is a simplified version of the challenge;
public final class Example {
private List<? extends Liquid> holder;
private List<List<? extends Liquid>> holder2;
public Example() {
holder = new ArrayList<>();
holder2 = new ArrayList<>();
}
public <T extends Liquid> void add(int position, T value) {
if (holder != null)
holder.add(position, value); //<- Error Here
}
public <T extends Liquid> void add(int position, List<T> value) {
if (holder != null)
holder.add(position, value); //<- This works
}
}
Client
I want the variable holder to store different subtypes
of Liquid. Example;
exampleInstance.add(0, new Water());
exampleInstance.add(1, new Juice());