• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Generic method exercise  RSS feed

 
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


 
author
Sheriff
Posts: 23569
138
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
Sheriff
Posts: 23569
138
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!