I have Java 8 generics question .
I have following class hierarchy:
Employee is base class
Manager extends Employee
Executive extends Manager
The following line (1) does not compile:
(1) List<? super Manager> foos1 = new ArrayList<Executive>(); // error
The below lines 2 and 3 compile ok:
(2) List<? super Manager> foos2 = new ArrayList<Manager>();
(3) List<? super Manager> foos3 = new ArrayList<Employee>();
however, even though I can still add Executive like so:
(4) foos2.add(new Manager("Manager!",123));
(5) foos3.add(new Executive("Executive!",1.0,2));
Please explain the logic why I can not assign variable
<? super Manager> = ArrayList<Executive>
where Executive is a super of Manager but I can still add Executive object to the array list?
many thanks!