File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bates & Sierra Practice Exams no.1 qn 58. revisited. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bates & Sierra Practice Exams no.1 qn 58. revisited." Watch "Bates & Sierra Practice Exams no.1 qn 58. revisited." New topic

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

Simon Forth

Joined: Nov 15, 2011
Posts: 20
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

Joined: Jan 28, 2008
Posts: 5575

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?
I agree. Here's the link:
subject: Bates & Sierra Practice Exams no.1 qn 58. revisited.
It's not a secret anymore!