File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
Leonardo Rafaeli
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
gutta rahul
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Braun
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic