aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes sierra/bates, ch2. question #14 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "sierra/bates, ch2. question #14" Watch "sierra/bates, ch2. question #14" New topic
Author

sierra/bates, ch2. question #14

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
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

Joined: Mar 12, 2011
Posts: 808
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

Joined: Oct 24, 2012
Posts: 95
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

Joined: Apr 04, 2012
Posts: 924
    
    1

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sierra/bates, ch2. question #14