• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Bates & Sierra Practice Exams no.1 qn 58. revisited.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've raised this problem before, got help, thought I understood the help I got and closed it.
However I realise I'm still confused by it.

Question 58 is as follows :-



Which can be inserted, independently at line 4, for the code to compile?
a. return e;
b. return e.getE();
c. return e2;
d. return e2.getE();
e. return new Hose().getE();
f. Compilaton failes regardless of which return is inserted.

The correct answer is f. as the doStuff() method is static. I don't have a problem with that.
However the book says that if doStuff() was non-static, a. and d. would be correct.
I can understand c. being wrong as then a Hose<E> is being returned instead of an E.
However I don't understand why b. is also wrong.

Henry Wong explained that the e is something that IS-A Hose and getE() will return something that IS-A Hose, but they may not be IS-A each other.
I understand the principle that two objects which both directly sub-class another class are not IS-A each other.
However I don't understand how that could occur in this situation.
Can someone give an example of what could give rise to this situation if option b. DID compile?

ALSO, if I may, changing line 1 to

enables the code to compile with line b.
However I don't know what "Hose <E extends Hose<E>>" really means - what does it mean?
And I am not able to instantiate a Hose object of this type - what is the correct syntax?

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, let's elaborate with example:


I write a subclass to Hose

at (1), you are passing subclass SubHose and Hose as argument and the method returns SubHose .
and now what is the valid return type among the arguments i.e, SubHose and Hose?
 
What's that smell? I think this tiny ad may have stepped in something.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic