• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Generics

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Q is why cannot i return an ArrayList as a return value when i can pass it as an argument to the method.if i understood correctly Polymorphism applies to "Base" not to the "Type".
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dont you think you can return a ArrayList of Integer,Short,Byte etc using this method signature.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Kumar Singh:
Dont you think you can return a ArrayList of Integer,Short,Byte etc using this method signature.



Using this signature, you should only be able to return something that implements a List of E, Number, or Object.

Henry
 
Ravi Nistala
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanjeev :
I also thought that i can return an "ArrayList " of either numbers,or integers and also as Wong mentioned above. But in the K&B book (the last question in collections) , it is said that you cannot return "ArrayList". It has to be "List".That is what i cannot understand.Am i missing something?
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ravi Nistala:

I also thought that i can return an "ArrayList " of either numbers,or integers and also as Wong mentioned above. But in the K&B book (the last question in collections) , it is said that you cannot return "ArrayList". It has to be "List".That is what i cannot understand.Am i missing something?



From the definition, it is indeed expecting that a List be returned. However, since an ArrayList implements the List interface, an ArrayList *is* an instance of a List -- and hence, an ArrayList may be returned as the List.

Henry
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you use K & B then always check The K & B Errata (as linked to at the top of this forum) You will see that the question is in error.
 
Ravi Nistala
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry for the info. I didn't know we have such a thing for the K&B book.
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic