• 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

Wildcard Type with Extend - Generics

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following three classes:



To learn about wildcards, I tested this code in a class as follows:



I get a compile error as follows:



I would be very grateful for any help in understanding why this is an error.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable "u" is of the class "Util<X>" where X is some unknown subclass of Employee. Its set() method expects a parameter of type X. It may not be possible to assign an Employee object to something of type X, so that's why it's illegal.
 
James Johnson Jr
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul Clapham, thanks for the quick reply.

Unfortunately, I don't quite understand why that would be an issue. My understanding is that Employee is a subclass of itself. So, if X is some subclass of Employee, then I'm not sure why it would be illegal to assign to X. For what it's worth, this also fails with the same type of error if I use Manager as follows:



Sorry if I'm misunderstanding your reply.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you saying that you believe that

is valid code whenever X is a subclass of Employee? And also that

is valid whenever X is a subclass of Employee?
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Johnson Jr wrote:Unfortunately, I don't quite understand why that would be an issue. My understanding is that Employee is a subclass of itself. So, if X is some subclass of Employee, then I'm not sure why it would be illegal to assign to X.



But X could be any subclass of Employee.

Look at it another way. Would you expect the following to compile?

That's essentially what's happening - or at least, it could be, if X is CodeMonkey. The compiler will only allow things it can guarantee are type safe, and that means type safe for any valid value of X.
 
James Johnson Jr
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for the replies.

I was viewing the set method as effectively becoming:



in the case of u. Then, I mistakenly thought that any parameter that extends Employee (such as Manager) should be able to pass. In other words the method would effectively be:



Instead, ? extends Employee represents a subclass of Employee. It is unknown which subclass so the call to set fails.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right. That's an extremely common misconception about wildcards in generics, so you aren't the first one to fall into that trap.
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my humble opinion, whenever you use a generic declaration as extends SomeThing that SomeThing should be of a type that is abstract and not possible to instantiate in itself.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The idea of the wildcard is not so much that the element is a subtype of something, but that the collection is a subtype of another.
So if we have public class Cherry extends Fruit, and List<Cherry> then List<Cherry> is not a subtype of List<Fruit>. But List<? extends Fruit> is a subtype of List<Fruit>.
It's in the Java Tutorials; look for "subtyping" and "wildcards".
 
James Johnson Jr
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the additional comments, but I have one concern in regards to:

Campbell Ritchie wrote:But List<? extends Fruit> is a subtype of List<Fruit>.



Shouldn't it be List<Fruit> is a subtype of List<? extends Fruit>?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have to go through the tutorials link I gave you earlier. Try this section too. The subtype and wildcard pages might explain it better. Sorry, I got confused because there are two generics sections in the tutorials.

I think you are probably correct. Have a look in the tutorials.
 
reply
    Bookmark Topic Watch Topic
  • New Topic