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

sierra/bates, ch2. question #14

 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is option A a valid answer to this question? In option A, the last line is "interface Floozert". The line before it says that class Floozel has a list of Floozets.

How can class Floozel have a list of Floozets, which in this option, are defined as interfaces? I thought interfaces are only implementable (by classes), and NOT standalone objects that can be added to a list.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel Glenn wrote:How can class Floozel have a list of Floozets, which in this option, are defined as interfaces? I thought interfaces are only implementable (by classes), and NOT standalone objects that can be added to a list.

An interface can not be directly instantiated, correct, but it still defines a Type, just as a class does. This is really useful: for any class that implements a particular interface, we can use that interface as the Type to refer to its instances. So we can have an interface Bottle with implementing classes GlassBottle and PlasticBottle. Then we can have a List<Bottle> and we can add instances of either GlassBottle or PlasticBottle by referring to them simply as Bottle.

Hope this helps.
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes thanks that is clear. Suppose, in this example, interface Floozet was NOT implemented by any classes. Then we could not add any objects to the list, right?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel Glenn wrote:Yes thanks that is clear. Suppose, in this example, interface Floozet was NOT implemented by any classes. Then we could not add any objects to the list, right?


Yes you are right
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic