• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Set that only Items can be added

 
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create a Set such that only Items can be added. Prove it.

I tried to do it Generic but as I expected a compile time error is thrown.
Is there another way of doing or it is just what the book is aking?



 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java is strongly typed language and the compiler error is a testament to that.

You could also try this, just for learning's sake - Now that your Set can only accept Items and since any class in Java automatically is a subclass of Object, you could use type casting and introduce an instance of Object into the Set. See what happens then.
 
reply
    Bookmark Topic Watch Topic
  • New Topic