This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

"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: 3401
    
    9
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".
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

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: 13884
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics
 
Similar Threads
Generics - nested type parameters
Simple J2SE 5.0 Tiger Notes
Casting an Object to a Class
Generic doubt :-)
Generics, multiple bounding types and efficiency