File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics in method return type 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 in method return type" Watch "Generics in method return type" New topic
Author

Generics in method return type

Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

I have a question regarding generics in method returntypes.

I found this question at JavaChamp (Dont continue reading if you plan to do the question and dont want the answer).

The following program contains two compilation errors, at what lines? (choose 2)


The correct answer is that Line 21 and Line 22 wont compile.
I have no problems with Line 22 but i dont get why Line 21 doesnt work and I couldnt find any example of this in the K&B Book.

The answer for Line 21 is that the method returns a List<? extends Chewable> and list2 is a List<Chewable>.
At the moment I cant see why this wont work? Anything that extends (implements) Chewable should be able to fit into a List<Chewable> ?

Perhaps I'm just tired but would appreciate if someone could shine some light on this.

Thanks.


OCPJP (310-065) - 88%
AmanZeeK Verma
Greenhorn

Joined: Dec 06, 2010
Posts: 29

To understand this, suppose, you have a following extra class


now printSize(list3) can return List<? extends Chewable>

If I am collecting the returned value as
But, this case is collecting it as List<Meat> (why not List<ChewingGum> ?? ..)

so, compiler canot ensure this.. unless you catch it properly (? extends Chewable)

Hope this can help to some extent!!..

EDIT/UPDATE: Oops, you already knew this (as mentioned in question)...
The previous line (printSize(line2)) is List<Chewable> ... Its again creating ambiguity for compiler. Remember in Runtime there will be Type Erasure... so JVM does not know about type of List!!..


Aman ZeeK Verma
Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

But imo its kinda the same as

However, in the above case I guess I would have to suit myself If I create a meatlist that accepts all kind of Chewables instead of typing it with <Meat> .

If I understand it correctly its the same reason as why you are not allowed to add anything to a list which is type <? extends SomeClass>?
Forexample:


You simply do not know what type of Chewable is in the list...
K&B talks about this, forgot it though since its only mentioned when the List is an argument to a method.

So... is it correct to say that if a method returns we know that noone can add new objects to the list?
M. White
Greenhorn

Joined: Dec 27, 2009
Posts: 18

I think it's because list2 can only accept List<Chewable>, as opposed to List<? extends Chewable> being returned by printSize(). Meaning it can only accept a list with a generic type of Chewable. Remember, having a wildcard (<?>) generic type can accept more than one type. Same with list3, it can only accept a list with a generic type of Meat.
Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

M. White wrote:I think it's because list2 can only accept List<Chewable>, as opposed to List<? extends Chewable> being returned by printSize(). Meaning it can only accept a list with a generic type of Chewable. Remember, having a wildcard (<?>) generic type can accept more than one type. Same with list3, it can only accept a list with a generic type of Meat.


Mmm, but what got me confused was that I thought that if printSize() (really stupid methodname by the way) would return a itshould should fit into but as K&B mentions, polymorphism doesnt apply to generics.

Just as is not legal.


Just to clarify...
If a method returns the variable which receives the return value has to be of type and nothing else, which in turn means that you are not allowed to add any elements to this list?
AmanZeeK Verma
Greenhorn

Joined: Dec 06, 2010
Posts: 29

Please consider Type Erasure and dry run on JVM

M. White
Greenhorn

Joined: Dec 27, 2009
Posts: 18

Just to clarify...
If a method returns

List<? extends SomeClass>

the variable which receives the return value has to be of type

List<? extends SomeClass>

and nothing else, which in turn means that you are not allowed to add any elements to this list?


I guess that's correct. or List<?>.
AmanZeeK Verma
Greenhorn

Joined: Dec 06, 2010
Posts: 29

http://www.coderanch.com/t/520359/java-programmer-SCJP/certification/Generic-return-type-wildCard

This discussion can also be helpful.
Malte Wannerskog
Ranch Hand

Joined: Jan 06, 2011
Posts: 92

Thanks all for helping me sorting this out!
 
wood burning stoves
 
subject: Generics in method return type