• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic