This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K7B Practice Exam no.1 q.58

 
Simon Forth
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here is the code properly formatted this time.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don Redd
Ranch Hand
Posts: 82
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I think of is, you can't use generics inside Static methods...
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it KB7 ?..from where you got this book ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthikeyan,

Thank you very much for your time and trouble.

I think that's solved this particular problem.

Thanks again.
 
Simon Forth
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic