File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes bounded wild card - confusion 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 "bounded wild card - confusion" Watch "bounded wild card - confusion" New topic
Author

bounded wild card - confusion

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Can any body explain me bounded wild card with any simple & short example .
thanks a lot .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See Section 4.1 of this Sun tutorial on generics...

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Thanks Marc ,
but I have 2 book but I am not getting the concept . Can any body explain me with short examlple .
thanks a lot .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Suppose we have a class hierarchy of cars: A Jetta is a Volkswagen that is a EuropeanCar that is a Car.

class Car {}
class EuropeanCar extends Car {}
class Volkswagen extends EuropeanCar {}
class Jetta extends Volkswagen {}

Now suppose that we define a class called Mechanic. Consider that a mechanic might work on all types of cars, or she might work only on European cars, or she might specialize in Volkswagens, or whatever. So we define a single Mechanic class and make it generic using a type parameter T to indicate what type of car that a particular instance of Mechanic works on.

class Mechanic<T extends Car> {}

Now suppose that we define a class called ServiceStation, which has various types of Mechanics as instance members.

One method a ServiceStation might perform is to replace wiper blades. In order to do this, we'll need a Mechanic, so we'll pass an instance of Mechanic to that method. Here, it doesn't really matter what type of Mechanic we use, so we'll use the wildcard question mark. Our method in ServiceStation looks something like this...

void replaceWipers(Mechanic<?> m) {}

But another method our ServiceStation might perform is to service a Jetta. Here again, we'll pass an instance of Mechanic to the method. But what type of Mechanic this time? Here, we need one that works on Volkswagens. So we specify a Mechanic with a bounded wildcard type: Mechanic<? extends Volkswagen>. And our method in ServiceStation looks something like this...

void serviceJetta(Mechanic<? extends Volkswagen> m) {}
[ February 02, 2005: Message edited by: marc weber ]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
void serviceJetta(Mechanic<?> m) { .... } then also it will work for Jetta ... I think .

thanks .
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by rathi ji:
void serviceJetta(Mechanic<?> m) {...} then also it will work for Jetta...

If we use an unbounded wildcard -- that is, <?> without "extends" or "super" -- then the method will accept any type of Mechanic. But a Mechanic<Car> or Mechanic<EuropeanCar> might not be qualified to work specifically on a Volkswagen. This is why we put an upper bound the wildcard.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
thanks marc . now it is clear .

first we put ? so it can take any object then we restrict it
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by rathi ji:
...first we put ? so it can take any object then we restrict it...

Well, just to be clear: It's not really "any object" -- it has to be an instance of the generic class we've specified (in this example, Mechanic).

The wildcard ? indicates that the generic can be of any type parameter. And if there are upper or lower bound restrictions, these are on the parameter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bounded wild card - confusion