Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set that only Items can be added

 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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?



 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic