• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

sierra/bates, ch2. question #14  RSS feed

 
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.
 
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?
 
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
Boost this thread!