GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Generics" Watch "Generics" New topic
Author

Generics

Ravi Nistala
Ranch Hand

Joined: Jul 11, 2002
Posts: 93



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

Joined: Nov 01, 2006
Posts: 381
Dont you think you can return a ArrayList of Integer,Short,Byte etc using this method signature.


~Sanjeev Singh<br />SCJP 1.5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ravi Nistala
Ranch Hand

Joined: Jul 11, 2002
Posts: 93
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
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ravi Nistala
Ranch Hand

Joined: Jul 11, 2002
Posts: 93
Thanks Barry for the info. I didn't know we have such a thing for the K&B book.
 
GeeCON Prague 2014
 
subject: Generics