So the function below is supposed to be a function that takes a Car
object and adds it to a Col<>
object.
void insertCar(List<? super Car> c, Car x) {
c.add(x)
}
The question asked whether if adding a variable n
of type Nissan
object to a variable c
of type List<Car>
would work (ie. insertCar(c, n)
)
The answer is yes but I'm not sure why. I thought adding subtypes of an object Car
would not be possible because of the use of super
. That it would only take types of type Car
or any supertype of Car
.
Anyone able to me understand?
EDIT Is it that...
I wouldn't be able to add a Nissan if the List<>
itself was of some other subtype being passed in? For example, if List<? super Car>
was actually List<? super Ford>
There seems to be conflicting answers below but this is a question provided for exam review so pretty sure the question and answer provided are correct. It's just my understanding of it is what I'm not sure about.