• Post Reply Bookmark Topic Watch Topic
  • New Topic

Proper use of generics??  RSS feed

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know if this is the proper way to accomplish my goal using generics. Basically I have 2 classes (ClassA & ClassB) and i want to create a function that will return either a List of ClassA or a list of ClassB....or possibly a mix of both depending on the parameter I pass. I figured in order to do this I would need to create this "Base" class that ClassA and ClassB would extend from that way I can use the generic wildcards. This works and it compiles, but I just want to know if this is the best way to accomplish my goal. Generics & wildcards really confuse the hell out of me so im trying to wrap my head around it with examples.

The reason i question it is because on my type casting in the main function i get type safety warnings and I would imagine i would not get those if i was doing this the "right" way
Type safety: Unchecked cast from List<capture#1-of ? extends Main.Base> to List<Main.ClassA>
Type safety: Unchecked cast from List<capture#1-of ? extends Main.Base> to List<Main.ClassB>

any input to my example would be greatly appreciated.
Thanks!!


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!