• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 23909
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: 23909
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 showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic