• Post Reply Bookmark Topic Watch Topic
  • New Topic

Lower Bounded Generics  RSS feed

 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!