• 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

Clarification about ? super Object

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example from book:

public void addAnimal(List<? super Dog> animals) {
animals.add(new Dog()); // adding is sometimes OK with super
}
public static void main(String[] args) {
List<Animal> animals = new ArrayList<Animal>();
animals.add(new Dog());
animals.add(new Dog());
AnimalDoctorGeneric doc = new AnimalDoctorGeneric();
doc.addAnimal(animals); // passing an Animal List
}

What does adding is SOMETIMES OK with super mean? (Pg. 594)
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

public void addAnimal(List<? super Dog> animals) {
//public void addAnimal(List<? extends Dog> animals) {
animals.add(new Dog()); // adding is sometimes OK with super
// animals.add(new Dog()); // adding is NEVER OK with extends
}
public static void main(String[] args) {
List<Animal> animals = new ArrayList<Animal>();
animals.add(new Dog());
animals.add(new Dog());
AnimalDoctorGeneric doc = new AnimalDoctorGeneric();
doc.addAnimal(animals); // passing an Animal List
}

What does adding is SOMETIMES OK with super mean? (Pg. 594)



I was also confused...But now I know what it means.

ASSUMPTIONS:
- Animal is a supertype of Dog
- subDog is a subtype of Dog
- You want to see if adding() any object to list passed into animals is possible


It means if you used "public void addAnimal(List<? extends Dog> animals){}", then you will never be able to add a dog object to the animals list. This is because you don't know what is passed in. So if a List<subDog> is passed in, then how could you add a Dog to it? You just can't because it only takes subDog type.

But with "List<? super Dog> animals" you really know it's not a List with a subDog that's passed in so you can add Dog. It just means you can see that the right operand of super, which is Dog in this case, could ALWAYS be added to whatever list that's passed in.

However, the description I gave above is only for the example above. It could be applied to other examples that are similar (i.e., methods that don't pass the animals list to another list in the same method).

But here is a method that has another List inside it (unlike example above):

import java.util.*;
public class BackLister{
//INSERT HERE
{
List<T> output=new LinkedList<T>();
for(T t: input)
output.add(0,t);
return output;
}}

Which of the following can be insterted at // INSERT HERE to compile and run without error?

A) public static <T> List<T> backwards(List<T> input)
B) public static <T> List<T> backwards(List<? extends T> input)
C) public static <T> backwards(List<? super T> input)
D) public static <T> List<? extends T> backwards(List<T> input)
E) public static <T> List<? super T> backwards(List<T> input)
F) public static <? extends T> backwards(List<T> input)
G) public static List<? super T> backwards(List<T> input)
H) public static <T> List <? super T> backwards (List<? extends T>input)

So in last example above, the opposite is true. In other words, no longer is List<? super T> allowed by the compiler in order to add. This is because the inner list doesn't take any super type of T. But List<? extends T> allows you to add as follows: output.add(0,t);
[ June 08, 2006: Message edited by: Firas Zureikat ]
reply
    Bookmark Topic Watch Topic
  • New Topic