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 Generic method exercise 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 "Generic method exercise" Watch "Generic method exercise" New topic
Author

Generic method exercise

Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 145

Hi everyone.

Studying the exercise that follows, I couldn't understand why LINE B and LINE C does not compile.
I'm getting very difficulty at questions that involves generics such this kind of question.

Could anybody help me with this?


Exercise from OCP Java SE 6 Programmer Practice Exams (Bert Bates & Katht Sierra)
Assessment Test 2: Question 11.



Thanks



Adolfo Eloy
Software Developer
OCPJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39


The method needs to return a Carpet of V, or a Carpet of something that inherits V; What is V will be decided when an instance of Carpet is instantiated. And it is not clear if B or C fulfills the IS-A V test.

Henry


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

Joined: Mar 21, 2009
Posts: 145

Thanks for reply Henry.

I'm getting confusing because if V extends B, should not the compiler knows that V can be anything that extends B (IS-A B)?

Tks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Adolfo Eloy wrote:
I'm getting confusing because if V extends B, should not the compiler knows that V can be anything that extends B (IS-A B)?


Yes. The compiler knows that V IS-A B.... but the compiler also knows that B may not be IS-A V. Since B may not be IS-A V, there is no guarantee that Carpet<B> IS-A Carpet<? extends V>.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic method exercise
 
Similar Threads
Cast with Runnable interface.
Generic types on methods and classes
need help about generics
Help with understanding Generics question from OCP Java SE 6 Programmer Practice Exams