I will try to simplify my compilation problem.
I have a Car Object and two successors called Honda & Toyota.
I have another Object called CarContainer which is defined as follows:
public class CarContainer<T> {} // doesn't do anyhthing
I want to create a list of car containers of many types of cars objects.
In this example it will hold an array of that type of cars.
List<CarContainer<? extends Car[]>> obs = new ArrayList<>();
This works fine -
CarContainer<Honda[]> hondaCarContainer = new CarContainer<>();
CarContainer<Toyota[]> toyotaCarContainer = new CarContainer<>();
Collections.addAll(obs, hondaCarContainer, toyotaCarContainer);
But when I try to generate the container using a method
private static <T> CarContainer<T[]> getCarContainer(T car) {
return null;
}
and calling
Collections.addAll( obs,
getCarContainer(new Honda()),
getCarContainer(new Toyota()));
My code doesn't compile.
(though this does
Collections.addAll(obs, getCarContainer(new Honda()));
and unbelivably this also
CarContainer<Honda[]> carContainer = getCarContainer(new Honda());
Collections.addAll(obs, carContainer, getCarContainer(new Toyota()));
which is the same. )
How is it solvable?