• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Proper use of generics??

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic