OCPJP 1z0-816

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
sreeharshitha
Posts: 4
Joined: Wed Jul 10, 2019 11:41 pm
Contact:

OCPJP 1z0-816

Post by sreeharshitha »

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: }

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: OCPJP 1z0-816

Post by admin »

On what line does it fail? What is the error message? What do you think is the issue?

sreeharshitha
Posts: 4
Joined: Wed Jul 10, 2019 11:41 pm
Contact:

Re: OCPJP 1z0-816

Post by sreeharshitha »

the compilation fails on lines 7 and 8. As Sparrow is a subclass of Bird, why can't be Sparrow object put into birds?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: OCPJP 1z0-816

Post by admin »

Please go through this write up:
viewtopic.php?f=2&t=473
It explains your doubt.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests