File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K7B Practice Exam no.1 q.58 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 "K7B Practice Exam no.1 q.58" Watch "K7B Practice Exam no.1 q.58" New topic
Author

K7B Practice Exam no.1 q.58

Simon Forth
Greenhorn

Joined: Nov 15, 2011
Posts: 20
public class Hose <E extends Hose> {
E innerE;
public static E dostuff(E e, Hose<E> e2) {
// insert code here
}
public E getE() {
return InnerE;
}
}

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.
Can anyone explain?
Simon Forth
Greenhorn

Joined: Nov 15, 2011
Posts: 20

Here is the code properly formatted this time.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Simon Forth wrote:
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.
Can anyone explain?


Basically, the variable e, is holding an E type, which is guaranteed to be something that IS-A Hose. However, this E type doesn't have a generic. So, when you call getE() on the e variable, it is not guaranteed to be the same type as the generic type.

In other words, and I agree that it is a tad confusing, while e is something that IS-A Hose and e.getE() will return something that IS-A Hose, the two types may not be IS-A each other. For the code to be valid, the object returned by e.getE() has to be guaranteed to be the same E type defined to be returned by the method.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

The reason I think of is, you can't use generics inside Static methods...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Don Redd wrote:The reason I think of is, you can't use generics inside Static methods...


Yeah, the OP mentioned that -- this question was "what if" the method was non-static.

Henry
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Is it KB7 ?..from where you got this book ?


OCA7
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

ankita modi. wrote:Is it KB7 ?..from where you got this book ?

from subject: K7B Practice Exam no.1 q.58 .

<edit>sorry ankita, I really fell on your question </edit>
Simon Forth
Greenhorn

Joined: Nov 15, 2011
Posts: 20
Ankita,

The book is the OCP Java SE 6 Programmer Practice Exams by Bert Bates and Kathy Sierra.


Henry,

While I'm writing I'd like to thank you for trying to help me with this.

I'm not sure understand your explanation at the moment, but I haven't found the time to sit and think about it really hard yet.

Thanks all the same.

Karthikeyan Kandasamy
Greenhorn

Joined: Apr 17, 2011
Posts: 14
Simon,

Able to use option b with following class (changed <E extends Hose> to class Hose <E extends Hose<E>>. This change somehow ensures that only E can be returned from getE method (my assumption).



To understand Henry's explanation, wrote the following code. In this example, Pipe and Pump extends Hose and overrides the getE method. Actual return type for both override method is Pipe object.

Pipe IS-A Hose.
Pump IS-A Hose.

but Pipe is not a Pump. When you call the getE method (passing Pump as E), it returns Pipe.



Simon Forth
Greenhorn

Joined: Nov 15, 2011
Posts: 20
Karthikeyan,

Thank you very much for your time and trouble.

I think that's solved this particular problem.

Thanks again.
Simon Forth
Greenhorn

Joined: Nov 15, 2011
Posts: 20
I think I spoke too soon.
Because I understood the notion that two objects which IS-A a third object are not necessarily IS-A each other, I thought that I would get to understand this problem, with a bit of effort.
However I'm still confused (to put it mildly).

Henry wrote :-
Basically, the variable e, is holding an E type, which is guaranteed to be something that IS-A Hose. However, this E type doesn't have a generic. So, when you call getE() on the e variable, it is not guaranteed to be the same type as the generic type.


I don't understand what 'this E type doesn't have a generic' means.
If it's remedied by putting then the question is :-
What does <E extends Hose<E>> mean exactly?
And what is the syntax you then need to instantiate a Hose object?
 
GeeCON Prague 2014
 
subject: K7B Practice Exam no.1 q.58