aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt on wildcards in generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt on wildcards in generics" Watch "doubt on wildcards in generics" New topic
Author

doubt on wildcards in generics

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
1) List<?> list = new ArrayList<Dog>();
2) List<? extends Animal> aList = new ArrayList<Dog>();
3) List<?> foo = new ArrayList<? extends Animal>();
4) List<? extends Dog> cList = new ArrayList<Integer>();
5) List<? super Dog> bList = new ArrayList<Animal>();
6) List<? super Animal> dList = new ArrayList<Dog>();

Hello Ranchers,
Can you please explain me the above wildcards?
I am facing problem to understand


Thanks<br />Dinesh
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi As per my understanding:
Generics are for compile time only,so at the compile time javac need to settle every thing, so when you write following:
/************************************************************/
1) List<?> list = new ArrayList<Dog>();
Compiler consider both <?> (on the LHS) and <Dog> (on the RHS)
<?> ---> anything that extends Object ---> So right hand side must comply with this and also it should confirm (means no ? on RHS in most of the cases)
as Dog extends Animal extends Object..So this compiles fine.
/*********************************************************/
2) List<? extends Animal> aList = new ArrayList<Dog>();
LHS(Left hand side):
< ? extends Animal > ---> Anything that extends Animal
Dog --> extends Animal --> compiles fine
/*********************************************************/
3) List<?> foo = new ArrayList<? extends Animal>();
<?> ---> anything extends Object
<? extends Animal> ---> Anything extends Animal (ahh.. nothing is sure..)
So should not compile
/*********************************************************/
4) List<? extends Dog> cList = new ArrayList<Integer>();
<? extends Dog> ---> anything extends Dog
<Integer> ---> doesn't extends Dog..so compiler error..
/*********************************************************/
5) List<? super Dog> bList = new ArrayList<Animal>();
<? super Dog> --> anything which is Superclassof Dog
<Animal> --> it is super class of Dog
so no problem.
/*********************************************************/
6) List<? super Animal> dList = new ArrayList<Dog>();
<? super Animal> -->anything which is super class of Animal
<Dog> --> Dog is subclass of animal not super class..!!
/*********************************************************/


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Thanks for expalining.
Now i got..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt on wildcards in generics