Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Generic method exercise

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Have you no shame? Have you no decency? Have you no tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic