File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
Ravi Nistala
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • 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".
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dont you think you can return a ArrayList of Integer,Short,Byte etc using this method signature.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry for the info. I didn't know we have such a thing for the K&B book.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic