(String[] args)
| 6 | |
| 7 | public class GenericsAndCovariance { |
| 8 | public static void main(String[] args) { |
| 9 | // Wildcards allow covariance: |
| 10 | List<? extends Fruit> flist = new ArrayList<>(); |
| 11 | // Compile Error: can't add any type of object: |
| 12 | // flist.add(new Apple()); |
| 13 | // flist.add(new Fruit()); |
| 14 | // flist.add(new Object()); |
| 15 | flist.add(null); // Legal but uninteresting |
| 16 | // We know it returns at least Fruit: |
| 17 | Fruit f = flist.get(0); |
| 18 | } |
| 19 | } |