I got a function that expects a LinkedList of objects, is it possible to pass and use a LinkedList of a sub-class of those objects?
public abstract class Car {...} //just for example
public class Audi extends Car {...}
public void showCars (LinkedList<Car> cars) { //example function
for (Car i : cars) {
System.out.println(i);
}
}
public void test () {
LinkedList<Audi> audis = new LinkedList<Audi>();
audis.add(new Audi(...)); //just for example so that the list is not empty
showCars(audis); //this says that its expecting a LinkedList<Cars>
//but got LinkedList<Audi>
}
It says that showCars expects LinkedList<Cars> but got LinkedList<Audi>. This is just a very simple code outline which hopefully shows my problem.