• 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

Generics

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,

If you are using generics, you should make sure that :


The only way to invoke that method is to pass Animal list as argument, no subtypes.
Or the other ways are to use the wildcard (extends & super, ?, conditions apply) ... but what about return type?



i saw questions that used generic methods and returned subtype of declared return type from methods.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Rafi,

It will not compile. The compiler slaps us in the face with message something like "Type mismatch: cannot convert from List<Dog> to List<Animal>"


This however compiles and works fine:

Now we declared that this method will return a List of Animals, but now it may also contain Animal subclasses like Dog.
 
Rafi Fareen
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Innar Made wrote:Hello Rafi,

It will not compile. The compiler slaps us in the face with message something like "Type mismatch: cannot convert from List<Dog> to List<Animal>"


This however compiles and works fine:

Now we declared that this method will return a List of Animals, but now it may also contain Animal subclasses like Dog.



thanks bro, i got it ;)
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic