File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Lower Bounded Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Lower Bounded Generics" Watch "Lower Bounded Generics" New topic
Author

Lower Bounded Generics

Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
Hi,

On this page, the following example is given of a lower bounded generic reference:



This declaration tells that along with Dog type, all other sub-types of Dog is also allowed. Not any of the super-type of Dog can be added including java.lang.Object. So, if we have classes like GoodDog and BadDog, both extending the Dog class, then the following statements are legal.


This description doesn't explain to me what the purpose of the wildcard is, because even if the wildcard is removed, the code is still legal:



Could anyone explain to me what exactly is the purpose of this wildcard: and how it differs from:

Thanks in Advance,
Dan
[ May 06, 2008: Message edited by: Dan Murphy ]

SCJP, SCJD, SCWCD
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
Not quite. The bit about <? super Dog> means you are going to give the whatever a particular generic type, but you haven't decided yet, but it must be a superclass of Dog. Object, Animal, LifeForm and Carnivore are included, Bloodhound, Bulldog and Pekinese are excluded because they are subclasses of Dog.

What you are describing is <? extends Dog>.

Go through the Java Tutorial about generics, paying particular attention to the bit about wildcards.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
[Campbell]: The bit about <? super Dog> means you are going to give the whatever a particular generic type, but you haven't decided yet, but it must be a superclass of Dog

To be clear, it must be a superclass of Dog, or Dog itself. (Depending which definition of "superclass" you use.)

Dan:

List<? extends Dog> - can be List<Dog>, List <Poodle>, List<Mastiff>, List<English Mastiff>, etc. Anything you get() from this list will be some sort of Dog. But you can't always put() a Dog into the list, because it might be a list of a different kind of Dog.

List<? super Dog> - can be List<Dog>, List<Mammal>, List<Animal>, List<LifeForm>, List<Object>, etc. You can always put() a Dog into this list. But if you get() something out, it might not be a Dog.
[ May 06, 2008: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Dan Murphy
Ranch Hand

Joined: Mar 29, 2005
Posts: 126
great explanation Jim - thanks!

But you can't always put() a Dog into the list, because if might be a list of a different kind of Dog.


Am I right in saying that you can never put any kind of dog into List<? extends Dog> because you never know what kind of dogs the list actually contains?
[ May 06, 2008: Message edited by: Dan Murphy ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
Back to the Java Tutorial which I quoted earlier. Have you read it?

It says that List<Lion> and List<Butterfly> extend List<? extends Animal>.

So . . .
List<Bulldog>, List<Sheepdog> and List<Dog> are regarded as subclasses of List<? extends Dog>.

So . . .
It appears you only need the ? wildcards when you want the different generic Lists to be subclasses of each other.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Lower Bounded Generics
 
Similar Threads
Questions about generics.
Generics - use of <? super <E>>
Generics
Generics question
Wildcards in Generics