• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

K7B Practice Exam no.1 q.58

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is the code properly formatted this time.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 82
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason I think of is, you can't use generics inside Static methods...
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it KB7 ?..from where you got this book ?
 
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

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
He does not suffer fools gladly. But this tiny ad does:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic