File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regarding 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 "regarding generics" Watch "regarding generics" New topic
Author

regarding generics

debasmita pattnayak
Ranch Hand

Joined: Apr 12, 2007
Posts: 94
hi all,

i am confused about the following stuff:



please explain me this...i am not sure about the lines in which i have put comments


debasmita
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The list is declared to accept instances of Animal.

Since Cat extends Animal, a Cat IS-AN Animal. Since Dog extends Animal, a Dog IS-AN Animal. So there is no problem adding these to the List.

The thing to consider is that everything you get out of the List will be a reference of type Animal, so if you intend to downcast back to Cat or Dog, you will need to take some precautions (e.g., check the actual type using instanceof before downcasting).
[ July 04, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
debasmita pattnayak
Ranch Hand

Joined: Apr 12, 2007
Posts: 94
hi marc,
thanks for the reply....
its clear now....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding generics