aspose file tools
The moose likes Beginning Java 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 Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 662

"Can we use wildcards to corrupt a Pair<Manager> through a Pair<? extends Employee> reference


No corruption is possible. The call to setFirst is a type error. To see why, let us have a
closer look at the type Pair<? extends Employee>. Its methods look like this:
"

Core Java Vol. I (8th Edition). P.633

Why do I get a compile time error when I run wildcardBuddiges.setFirst to lowlyEmployee? What do you think the author referred to as lowlyEmployee?



Jon
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3883
    
  18

Presumably, lowlyEmployee is an instance of Employee or a subclass of Employee, but not an instance of Manager.

It gives an error because you're not allowed to add just *any* kind of Employee to a Pair that *could be* a Pair of Managers (as it is, in this case).

Pair<? extends Employee> means, "A pair of something that extends Employee, but I don't know what! It could be a Pair<Employee>, or a Pair<Manager>, or even some other kind of Employee I don't know about".


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 662

Stephan van Hulst wrote:Presumably, lowlyEmployee is an instance of Employee or a subclass of Employee, but not an instance of Manager.

It gives an error because you're not allowed to add just *any* kind of Employee to a Pair that *could be* a Pair of Managers (as it is, in this case).

Pair<? extends Employee> means, "A pair of something that extends Employee, but I don't know what! It could be a Pair<Employee>, or a Pair<Manager>, or even some other kind of Employee I don't know about".


Hence, if lowlyEmployee is an Employee, why would the compiler complain?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14910
    
  26

Your Pair class is defined in such a way that both the objects that you store in the pair must be of the exact same class.

When you have a Pair<? extends Employee> then the compiler cannot check that when you set values in the pair, they are indeed of the same class. You could call setFirst() with a Manager and setSecond() with an Employee, and then there would be a problem because Manager and Employee are two different classes. So to make sure you cannot do this the compiler simply gives an error if you try.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics
 
It's not a secret anymore!