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: }
OCPJP 1z0-816
Moderator: admin
-
- Posts: 4
- Joined: Wed Jul 10, 2019 11:41 pm
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: OCPJP 1z0-816
On what line does it fail? What is the error message? What do you think is the issue?
-
- Posts: 4
- Joined: Wed Jul 10, 2019 11:41 pm
- Contact:
Re: OCPJP 1z0-816
the compilation fails on lines 7 and 8. As Sparrow is a subclass of Bird, why can't be Sparrow object put into birds?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Who is online
Users browsing this forum: No registered users and 7 guests