aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

Leonardo Rafaeli
Ranch Hand

Joined: Dec 12, 2005
Posts: 58
Hi everybody

i have many questions about generics...

and I don't understand some explanations, so, if anybody can help me, I will be grat ^^

let's go

<T extends XXX> and <T super XXX>.. what is the diference??? in first, T is a subclass of XXX and in second T is a parent of XXX???

and about the <? ???
ex:
<? extends XXX>

what is this ? ? represents "any class"? I really confused with this crazy generics

[]�s


SCJP 1.4, SCWCD 1.4, SCBCD 5, OCUP, SCEA 5 (I), Oracle SOA Implementation Specialist, IBM Websphere Lombardi 7.1 Fundamentals
Blog: [url]http://www.leozin.com.br/blog[/url]
gutta rahul
Greenhorn

Joined: Nov 23, 2003
Posts: 12
In <? extends XXX>, "?" represents any type which is child of XXX. Suppose there are B, C and D child classes for base class A, then <? extends A> represents that any of the classes types B, C and D can be passed to "?" which is bounded by base class A.

Iam preparing for 1.5 certification and, if there is any worng in the above explination, please correct me.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

I think one of the main questions and confusion that can occur is really understanding the difference between

<T extends XXX>

and

<? extends XXX>

In some ways they are the same thing, but they are used in different contexts.

When you are creating something Generified, like a template class or template interface, you generally have no idea what the actual implementation class will want to use, and you define a letter, to be overwritten later in an actual implementation class. That letter being "T" which generally stands for Type.

Now, lets say you have a class that has a method that you are not sure who will be calling it, what type it will be passing it, and you are not generifying the method or class, but just want to allow someone to call it. That is when you use the "?" style.

So let's look at the Collection Classes. Instead of using T, they use E. What they are doing is generifying the Collection Class, it is not an actual instantiated object. When you instantiate a List, you define what "E" stands for as in List<MyObject> is a List of MyObjects.

so here is an example. I am going to generify a method in an interface



in that case I cannot use a "?". It isn't allowed.

now I am actually implementing the interface, and I have



Now I can use the ?, and I cannot use T or S because I am not generifying the method I implementing a method, in which I don't know which type of List they will send to me. It can be a List of Longs or a List of Numbers. The only thing I now can't do is add to the List that is being passed to my real getSomething method. Another Topic.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Peter Braun
Ranch Hand

Joined: Feb 09, 2005
Posts: 57
Mark, I don't understand something about your second example.

public class MyClass implements MyInterface<String, Long> {


Below this there is <? extends Number>.

If you don't know what type of List is the type of the someParameter, why did you use Long in method declaration?


Sorry for my incomprehension.

Peter
[ December 22, 2005: Message edited by: Peter Braun ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Long extends Number. So I wanted to make sure I had a class that would be correct and extends Number.

Mark
Balaji Sampath
Ranch Hand

Joined: Sep 30, 2005
Posts: 63
Hey Marc,
So is there anyway we can also generalize in the class declaration ..
public class MyClass implements MyInterface<String, ?>

regards
Balaji.S
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Originally posted by Balaji Sampath:
Hey Marc,
So is there anyway we can also generalize in the class declaration ..
public class MyClass implements MyInterface<String, ?>

regards
Balaji.S


No, because what does that mean. "?" who is ever going to define what "?" is or might be. Your Myclass is the only place it will ever be defined. But, of course, the best way to see for yourself it to try and see what happens.

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Basically ? is an implementation symbol and not a declaration symbol. I will post the compile error that shows when you try to use ? in the class definition.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics