Simple example:
public class Person
{
String name;
}
public class VIP extends Person
{
String title;
}
And then doing:
public static void main(String[] args)
{
Person p = new Person();
p.name = "John";
VIP vip = new VIP();
vip.name = "Bob";
vip.title = "CEO";
List<Person> personList = new ArrayList<Person>();
List<VIP> vipList = new ArrayList<VIP>();
personList.add(p);
personList.add(vip);
vipList.add(vip);
printNames(personList);
printNames(vipList);
}
public static void printNames(List<Person> persons)
{
for (Person p : persons)
System.out.println(p.name);
}
gives an error on "printNames(vipList)" (required List<Person> found List<VIP>).
Does this mean that although VIP is a Person, List<VIP> is not a List<Person>?