OCPJP 1z0-816
Posted: Sun Aug 11, 2019 11:47 pm
I am preparing for 1z0-816 from Boyarsky/Selikoff book and came across this code. the explanation says lines 7 and 8 do not compile. I cannot understand why it doesn't compile because List<? extends Bird> can accept List<Bird> or List<Sparrow>.
2: static class Sparrow extends Bird{}
3: static class Bird{}
4:
5: public static void main(String... args){
6: List<? extends Bird> birds = new ArrayList<Bird>();
7: birds.add(new Sparrow());
8: birds.add(new Bird());
9: }
2: static class Sparrow extends Bird{}
3: static class Bird{}
4:
5: public static void main(String... args){
6: List<? extends Bird> birds = new ArrayList<Bird>();
7: birds.add(new Sparrow());
8: birds.add(new Bird());
9: }