aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic with a lower bounded wildcard, ? super Dog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic with a lower bounded wildcard, ? super Dog" Watch "Generic with a lower bounded wildcard, ? super Dog" New topic
Author

Generic with a lower bounded wildcard, ? super Dog

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86


The return parameter is Mixer so if is a defined with a lower bounded wildcard.
Why do I have a compiler error when I return a Mixer<Object> and there is no compiler error with Mixer<Animal>?
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

I already have the answer of the book but it doesn't clear my doubt. It says that because return type of the method is Mixer<? super Dog>, which means a Mixer object with a generic type that is either a Dog or a supertype of Dog. But in this case Object is a supertype and there is a compiler error.
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

Both the return statments would have worked fine, because, both Animal and Object is a super-type of Dog
But, the reason why the first Mixer<Object> does not fits in is because, we have declared your class as: -

So, you have bound your type that can be associated with Mixer class to either Animal or its subtype. Now, since, Object is not a subtype of Animal, you can't just create:

So, we can create instances of your class like:
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1760
    
    7

Look carefully at the declaration of the Mixer class, not the return type declaration, what kind of type parameters does it accept?

Edit: too slow...


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic with a lower bounded wildcard, ? super Dog