Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic with a lower bounded wildcard, ? super Dog

 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic