aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about 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 "Question about generics" Watch "Question about generics" New topic
Author

Question about generics

Jim Pouwels
Ranch Hand

Joined: Feb 22, 2012
Posts: 61
Hi,

I've encountered the following code in the OCJP mock test from the K&B study guide book attached CD, and I think this is not covered in the Study Guide:



I'm really not getting why you should write ? extends T or ? super T. What are the implications? T is defined nowhere?

? super Cat makes sense to me. This implies that you can add Cats to the list passed in.
? extends Animal also makes sense. This implies you can iterate any incoming list using the Animal object type.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Jim Pouwels wrote:Hi,

I've encountered the following code in the OCJP mock test from the K&B study guide book attached CD, and I think this is not covered in the Study Guide:



I'm really not getting why you should write ? extends T or ? super T. What are the implications? T is defined nowhere?

? super Cat makes sense to me. This implies that you can add Cats to the list passed in.
? extends Animal also makes sense. This implies you can iterate any incoming list using the Animal object type.


You need to read up on generics -- specifically, generic methods. The T type is determined by the compiler when the method is used -- it is determined at compile time, but not during the compilation of this code.

As for "? extends" and "? super", those are wildcards. And the "implication" is that with wildcards you can write methods that support more parameter types. In general, if you have methods where you don't really care too much of the type (and can operate without knowing the type too much), you should use wildcards, as it allows your method to be more applicable.

Henry


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

Joined: Feb 22, 2012
Posts: 61
Still unclear to me. Say I have the following code:



As far as I know I can still pass a list with any generic type. Where is the restriction? What invocation will fail in this case?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Jim Pouwels wrote:Still unclear to me. Say I have the following code:



As far as I know I can still pass a list with any generic type. Where is the restriction? What invocation will fail in this case?



You can pass any type, because that is how you used it... let's say you actually use the returned list, and assigned it to a list of animals. In that case, T will be typed as Animal. And your method will work for Cat, if it had been the wildcard that extends T.

If you didn't use wildcards, or use the version that allows the "super", you would have gotten a compilation error.


Henry
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi Jim,
Henry Wong wrote:
Jim Pouwels wrote:Still unclear to me. Say I have the following code:





I think the question is about what can be passed as argument and what can be returned from the backwards method.
Case 1 :


Case 2 :


Correct me if I am wrong.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about generics
 
Similar Threads
Generic
Generics
Generic Method from K&B Master Exam
Doubt
Help me understand