Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic method exercise

 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Henry Wong
author
Marshal
Pie
Posts: 21214
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21214
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic