Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mock exam question

 
pritam kamkar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi can anybody please tell me whether this program will compile?



i think it will not compile becuase we can not add anything when we use the extends keyword
please correct me if i am wrong
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try compiling the code? Of course compiler won't let you add anything. By <? extends Instrument> you are saying "I sware not to add anything to this list"

Regards.
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha wrote:
Of course compiler won't let you add anything
That's a common mistake. You can always add "null": allInstruments.add(null) compiles fine.
[ July 28, 2008: Message edited by: Ralph Jaus ]
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I forgot about it.you can add "null" there.
[ July 28, 2008: Message edited by: Vijitha Kumara ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic