generics
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hi all... can somebody tell me the difference between <? extends Animal> and <? super Animal> with example.... i am slightly confused...
thanks in advance...
thanks in advance...
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
There two mayor difference. The wildcard (?) is used to say "any class", so <? extends Animal> would something like "any class that extends Animal" and <? super Animal> would be "any class that is a superclass of Animal".
For example, let's suppose that Dog extends Animal and Cat extends Animal. And we know that Animal extends Object (implicitly).
So, we could do this:
List<? extends Animal> list1 = new ArrayList<Dog>(); (it's OK, Dog extends Animal)
List<? extend Animal> list2 = new ArrayList<Cat>(); (it's OK, Cat extends Animal)
List<? extends Dog> list3 = new ArrayList<Animal>(); (NOT OK)
List<? super Dog> list4 = new ArrayList<Animal>(); (it's OK, Animal is a superclass of Dog)
Notice that wildcards can ONLY by used on declaration. These is absurd:
List<? extends Animal> list1 = new ArrayList<? extends Animal>(); (Horrible wrong)
So, why is there that difference? Well, if you declare a class with a generic using the wildcard that extends another class, you can't ADD anything to it. This will result as a big compile error:
List<? extends Animal> list1 = new ArrayList<Dog>();
list1.add(new Dog()); (Compile error)
But, you can add to a instance declare with the wildcard that supers another class:
List<? super Dog> list1 = new ArrayList<Animal>();
list1.add(new Dog()); (It's good)
I hope this clears out your question.
For example, let's suppose that Dog extends Animal and Cat extends Animal. And we know that Animal extends Object (implicitly).
So, we could do this:
List<? extends Animal> list1 = new ArrayList<Dog>(); (it's OK, Dog extends Animal)
List<? extend Animal> list2 = new ArrayList<Cat>(); (it's OK, Cat extends Animal)
List<? extends Dog> list3 = new ArrayList<Animal>(); (NOT OK)
List<? super Dog> list4 = new ArrayList<Animal>(); (it's OK, Animal is a superclass of Dog)
Notice that wildcards can ONLY by used on declaration. These is absurd:
List<? extends Animal> list1 = new ArrayList<? extends Animal>(); (Horrible wrong)
So, why is there that difference? Well, if you declare a class with a generic using the wildcard that extends another class, you can't ADD anything to it. This will result as a big compile error:
List<? extends Animal> list1 = new ArrayList<Dog>();
list1.add(new Dog()); (Compile error)
But, you can add to a instance declare with the wildcard that supers another class:
List<? super Dog> list1 = new ArrayList<Animal>();
list1.add(new Dog()); (It's good)
I hope this clears out your question.
SCJP 5, SCWCD 5
sweety singh
Ranch Hand
Posts: 49
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hey thanks for that reply... but i want to ask you one thing... in the following code..
Code:
public void addAnimal(List<? extends Animal> animals)
{
animals.add(new Dog()); //1
}
in line 1.. the book says "No! can't add if we use <? extends Animal>"
why is it so...
[ May 25, 2008: Message edited by: sweety singh ]
[ May 25, 2008: Message edited by: sweety singh ]
Code:
public void addAnimal(List<? extends Animal> animals)
{
animals.add(new Dog()); //1
}
in line 1.. the book says "No! can't add if we use <? extends Animal>"
why is it so...
[ May 25, 2008: Message edited by: sweety singh ]
[ May 25, 2008: Message edited by: sweety singh ]
Daniel Del Moral
Ranch Hand
Posts: 32
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You can't add to prevent adding the wrong object to the list. For example:
public void addAnimal(List<? extends Animal> animals)
{
animals.add(new Dog()); //1
}
Imagine for a minute that that compiles fine. What happens if I called this method like this:
addAnimal(new ArrayList<Cat>());
Cat extends Animal, so it would be fine, but you did you noticed that I added a Dog? So the adding restriction is there to prevent this circumstances.
public void addAnimal(List<? extends Animal> animals)
{
animals.add(new Dog()); //1
}
Imagine for a minute that that compiles fine. What happens if I called this method like this:
addAnimal(new ArrayList<Cat>());
Cat extends Animal, so it would be fine, but you did you noticed that I added a Dog? So the adding restriction is there to prevent this circumstances.
SCJP 5, SCWCD 5
sweety singh
Ranch Hand
Posts: 49
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
but dog also extends animal.... i am confused... i did not understand..
posted 17 years ago
Yes it does. The point is that you cannot be sure what will be added if the code were allowed to be compiled. Just remember that when you see the ? symbol you cannot add anything to the data structure
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by sweety singh:
but dog also extends animal.... i am confused... i did not understand..
Yes it does. The point is that you cannot be sure what will be added if the code were allowed to be compiled. Just remember that when you see the ? symbol you cannot add anything to the data structure
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
So you say that any class that uses generic wild card declaration cannot able to add any thing??
even in the case of it extends another class or it superclass of another??
even in the case of it extends another class or it superclass of another??
sweety singh
Ranch Hand
Posts: 49
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hey thanks all... i have understood...
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi,
Can't really agree on that. As Daniel Del Moral explained in his post,
is perfectly fine.
Best regards,
- Aditya
Just remember that when you see the ? symbol you cannot add anything to the data structure
So you say that any class that uses generic wild card declaration cannot able to add any thing??
even in the case of it extends another class or it superclass of another??
Can't really agree on that. As Daniel Del Moral explained in his post,
is perfectly fine.
Best regards,
- Aditya
posted 17 years ago
I was commenting on the extends scenario. I guess I forgot to mention that
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Can't really agree on that
I was commenting on the extends scenario. I guess I forgot to mention that
| They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |









