I am working on upper bound wild card( ? ) in arraylist. it is not working when i declare an arraylist using wildcard. For Example : i have three classes:
1) Person class:
public class Person {
}
2) employee class which extends person:
public class Employee extends Person {
}
3) dept class which extends employee.
public class Dept extends Employee{
}
now i want the objects of these three classes should be add to arraylist in a seperate class as below.
public class TestPerson {
public static void main(String[] args) {
Person p1 =new Person();
Employee e1 = new Employee();
Dept d1 = new Dept();
ArrayList<? extends Person> al =new ArrayList<Person>();
al.add(p1);
al.add(e1);
al.add(d1);
}
}
The last three lines in the code showing me the following error: The method add(capture#1-of ? extends Person) in the type ArrayList is not applicable for the arguments (object type).
But from arraylist of type "? extends person", can take "any object extends person" , but why its not accepting employee object which extends person.