Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

question about generics

Thread view: 
puzzlecracker - 15 Jan 2006 17:50 GMT
What is the difference  between  "? super SomeType" and  "? extends
SomeType" ?  

Thanks
Thomas Hawtin - 15 Jan 2006 18:01 GMT
> What is the difference  between  "? super SomeType" and  "? extends
> SomeType" ?  

Perhaps an example will help.

The possible types for ? super java.lang.Number are:

   Object, Number and java.io.Serializable.

The possible types for ? extends Number include:

   Number, Byte, Short, Integer, Long, Float, Double,
     BigInteger, BigDecimal, AtomicInteger and AtomicLong

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

puzzlecracker - 15 Jan 2006 18:18 GMT
> > What is the difference  between  "? super SomeType" and  "? extends
> > SomeType" ?
[quoted text clipped - 14 lines]
> Unemployed English Java programmer
> http://jroller.com/page/tackline/

No sure if this is true; quote from the book (Core Java, p.732)

     Executive==>Manager==>Employee

? super Manager
This wildcard restricts to all super types of Manager
Thus,
Par<? super Manager>
 void set (? super Manager)
 ? super Manager get()

The Compiler doesn't know the exact type of  the set method by can call
it with any Manager or subtype (such as Executive), but not with
Employee. But you have no guarantee about returned type. You can only
assign it to an Object.

----
That contradicts your statements that only supertypes of  Manager can
be passed in.

Alas, I don't how it is different from extend?

Thanks
Chris Smith - 15 Jan 2006 19:43 GMT
> The Compiler doesn't know the exact type of  the set method by can call
> it with any Manager or subtype (such as Executive), but not with
[quoted text clipped - 4 lines]
>  That contradicts your statements that only supertypes of  Manager can
> be passed in.

That's not what Thomas said.  The wildcard type ? super Manager can be
resolved to Manager or any supertype.  Remember that even without
wildcards, you could still pass in an Executive reference to a function
that expects a Manager, because a reference of type Executive is
assignable to the more general type Manager.  Therefore, it's safe to
pass in an Executive to a function expecting <? super Manager>
specifically because the function can't be expecting anything more
specific than Manager.

> Alas, I don't how it is different from extend?

super and extends are exact opposites in wildcard expressions.  One
establishes an upper bound, while the other establishes a lower bound.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

puzzlecracker - 15 Jan 2006 21:37 GMT
> > The Compiler doesn't know the exact type of  the set method by can call
> > it with any Manager or subtype (such as Executive), but not with
[quoted text clipped - 25 lines]
> Chris Smith - Lead Software Developer/Technical Trainer
> MindIQ Corporation

I might perhaps be utterly confused and in need further
explaination(s), for I am coming directly from c++ templates, and these
concepts are way foreign...
puzzlecracker - 15 Jan 2006 21:40 GMT
> > The Compiler doesn't know the exact type of  the set method by can call
> > it with any Manager or subtype (such as Executive), but not with
[quoted text clipped - 15 lines]
>
> > Alas, I don't how it is differen

IF THAT WAS true, then ? super manager  should all to accept  Employee
class since it is still in the set of super. right?
Chris Smith - 15 Jan 2006 21:55 GMT
> IF THAT WAS true, then ? super manager  should all to accept  Employee
> class since it is still in the set of super. right?

No.  The method has a single formal parameter.  The type of that formal
parameter might be Manager, or it might be Employee, or it might be
java.lang.Object, since that's the superclass of everything.  If you
pass an actual parameter of type Employee but the formal parameter type
is Manager (which is a possibility) then you've violated type safety, so
the compiler won't allow that.

On the other hand, if you pass an Executive, then the compiler knows
that it can safely upcast that parameter to type Manager OR Employee OR
Object... so it's safe.

The way this differs from <? extends Manager> is as follows.  In the
latter case, the formal parameter might be Manager, or it might by
Executive, or MiddleManager, or LineManager, or it any other class
you've never even heard of that happens to extend Manager.  It's not
safe to pass anything to that method because the method might be
expecting something else... so it's actually illegal to call the method
at all.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

puzzlecracker - 15 Jan 2006 22:24 GMT
> > IF THAT WAS true, then ? super manager  should all to accept  Employee
> > class since it is still in the set of super. right?
[quoted text clipped - 24 lines]
> Chris Smith - Lead Software Developer/Technical Trainer
> MindIQ Corporation

Thanks. I think I got, albeit I need more digestions
puzzlecracker - 15 Jan 2006 22:56 GMT
> > > IF THAT WAS true, then ? super manager  should all to accept  Employee
> > > class since it is still in the set of super. right?
[quoted text clipped - 26 lines]
>
> Thanks. I think I got, albeit I need more digestions

Still don't get it......I think with ? super you can still corrupt.
No?

for example

Pair<SuperManager> =new Pair<me,you>
puzzlecracker - 15 Jan 2006 22:58 GMT
> > > IF THAT WAS true, then ? super manager  should all to accept  Employee
> > > class since it is still in the set of super. right?
[quoted text clipped - 26 lines]
>
> Thanks. I think I got, albeit I need more digestions

Still don't get it......I think with ? super you can still corrupt.
No?

for example

Pair<SuperManager> superBuddies= new Pair<SuperManegers>(me,you);
Pair<?super Manager> buddies =superBuddies;
buddies.setFirst(new Manager("lowey"));

scream! VIOLATION
Chris Smith - 16 Jan 2006 00:18 GMT
> Still don't get it......I think with ? super you can still corrupt.
> No?
[quoted text clipped - 6 lines]
>
> scream! VIOLATION

So what is the class hierachy here?  We haven't discussed this
SuperManager class.  If Manager extends SuperManager, then this is valid
code and is type-safe.  Otherwise, the second line doesn't compile.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

puzzlecracker - 15 Jan 2006 22:01 GMT
> > The Compiler doesn't know the exact type of  the set method by can call
> > it with any Manager or subtype (such as Executive), but not with
[quoted text clipped - 15 lines]
>
> > Alas, I don't how it is differen

IF THAT WAS true, then ? super manager  should all to accept  Employee
class since it is still in the set of super. right?
Lasse Reichstein Nielsen - 16 Jan 2006 02:09 GMT
>  quote from the book (Core Java, p.732)
...
>       Executive==>Manager==>Employee
...
> Par<? super Manager>
>   void set (? super Manager)
>   ? super Manager get()
...
>  That contradicts your statements that only supertypes of  Manager can
> be passed in.
...
> ----

Notice that there are three kinds of types being used here.

Take this example:
Set<? super Manager> someSet = new HashSet<Employee>();
someSet.add(new Executive("John Smith");

You can see Set<? super Manager> as a kind of "type pattern".
It is not a type of a value in itself, but rather it specifies a
set of possible types { Set<X> | X is a supertype of Manager }
(where supertypes of Manager in this example are only Manager itself,
Employee and Object - for simplicity we include a class as a trivial
supertype of itself :).

You can assign any value to someSet that has a type in this set.
HashSet<Employee> is one possible value. HashSet<Manager> is another.

Notice that you cannot write
new HashSet<? super Manager>
The type parameter must be an actual type, not just a set of types.
This is somewhat similar to how you can't call a method with a type
as argument, only with an actual value of that type.


The choice of parameter type for HashSet also affects the signatures of
its methods. One example is the put method, which for HashSet<Employee>
has signature
 void put(Employee value)

Since Executive is a subclass of Employee, an instance of Executive
can be used as parameter of type Employee. This is the fundamental
substitution property of class based languages: an instance of a
subclass can be used everywhere the superclass can be.

In the above example, we don't know the exact type of the variable
someSet, only the set of types that it might have. So we don't know
the exact signature of "put", just that it is
 void put(T value)
where T is some supertype of Manager. So we don't know exactly what
objects can be used as parameters to it. We do know that the type
is a supertype of Manager, so any instance of Manager, or any subtype
of Manager, must be elements of that type. This is why the compiler
only allows objects that are Managers.

It seems a little backwards: the type must be a superclass, so the
value must be of a subclass. But when you understand it, it's really
the only thing that makes sense :)

/L 'what is the type of a type parameter?'
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

puzzlecracker - 16 Jan 2006 02:34 GMT
> >  quote from the book (Core Java, p.732)
> ...
[quoted text clipped - 60 lines]
>  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
>   'Faith without judgement merely degrades the spirit divine.'

That was rather informative.........thx


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.