Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Simon Forth
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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?

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic