• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

regarding generics

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi marc,
thanks for the reply....
its clear now....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!