This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics " Watch "Generics " New topic
Author

Generics

Rafi Fareen
Ranch Hand

Joined: Aug 28, 2009
Posts: 75
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.
Innar Made
Greenhorn

Joined: May 12, 2009
Posts: 17
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

Joined: Aug 28, 2009
Posts: 75
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 ;)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics
 
Similar Threads
Generics
Generics
Generics
Generics
Generics