Generics
Submitted by Annu on Fri, 09/12/2008 - 21:30.
Hi,
As far as I have understood
List<? super Number>
means this accepts any List with a generic type that is of type Number, or a supertype of Number. Nothing lower in the inheritance tree can come in, but anything higher than Number is OK. Then why does it allow to add an Integer into the list, doesn't this addition violate the above statement "Nothing lower in the inheritance tree can come in, but anything higher than Number is OK".
Thanks in advance.
Regards
Annu
Annu,
In your example the List is of a generic type which has Number as it's subclass. Now since this generic type is the parent of Number it also features in the type hierarchy of the classes that are subclasses of Number.
Ex: Lets say the new type is T then the class hierarchy of T is as follows
Parent: T
|-- Number
|-- Short
|-- Integer
|-- Float
and so on...
Now, since the list can only accept objects which are of the new generic type "T" we are free to add any subclasses of Number, which includes Integer.
Note that by saying <? super Number> we're not saying super classes of Number(as defined for the class), we're saying any & all subclasses of Number. Think of it as simply List.
And yes - "Nothing lower in the inheritance tree can come in, but anything higher than Number is OK" is an incorrect statement. Here's a link to Sun's tutorial on Generics, see if it helps further - http://java.sun.com/docs/books/tutorial/java/generics/index.html
HTH
Ashish Hareet
iRix Technlogies
Nicely Put Annu....
Thanks. Now that makes some sense. Had I known about this I wld have liked this generics beast more...
Thanks
import java.util.*;
class Animal{}
class Dog extends Animal{}
class BulDog extends Dog{}
public class GenTest{
public static void main(String [] args){
List<? super Dog> list = new ArrayList();
// list.add(new Animal()); // Compilation error
list.add(new Dog());
list.add(new BulDog());
}
}
Here "List<? super Dog> list" says that list is a variable of any base type which
IS-A List,like ArrayList and the type is Dog class or any of its superclasses.
Type can be Object,Animal,Dog etc., but not any subtype of Dog.
So the list definitely can take any object of Dog or its subclass
And may not accept a superclass object of Dog class
angle bracket is not working properly so i am using / / in place of angle bracket
please consider that...........
-----------------------------------------------
List /Object/ list = new ArrayList/Dog/();//Really not possible but think so virtually
list.add(new Animal()); //OK
list.add(new Dog());//OK
list.add(new Dog());//OK
-----------------------------------------------
List/Animal/ list = new ArrayList/Dog/();//Really not possible but think so virtually
list.add(new Animal()); //OK
list.add(new Dog());//OK
list.add(new Dog());//OK
-----------------------------------------------
List/Dog/ list = new ArrayList/Dog/();
list.add(new Animal()); // Compilation error
list.add(new Dog());//OK
list.add(new Dog());//OK
-----------------------------------------------
To work fine in all scenario compiler takes only Dog or any of its subtype object.
-Arijit Daripa,
Preparing for SCJP CX 310-055
Arijit,
Thanks for stopping by my site. The angle brackets require the html syntax, you can read about how to use it here - http://www.irixtech.com/filter/tips.
Time to find a better editor for my forums.
Ashish Hareet