• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

regarding doStuff()

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Which three, inserted independently at line 8, will compile? (Choose three.)

// A and B have the same return type don't they? Why does A work and B doesn't?



A
int doStuff() { return 42; }
B
int doStuff(int x) { return 42; }
C
Foo doStuff(int x) { return new Foo(); }
D
Object doStuff(int x) { return new Object(); }
E
SuperFoo doStuff(int x) { return new Foo(); }







courtesy of tmn.sun.com
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A and B have different parameter types. One is an overloaded method, the other an incorrectly overridden method.
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In B, you are attempting to override the super class method so you cannot change the return type. In A you are not overriding the super class method because your method has a different signature (no parameter) so you can change the return type.
 
Rob Spoor
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote:In B, you are attempting to override the super class method so you cannot change the return type.


Not 100% true. You can change the return type to a sub type; that's why C is allowed. This is called covariant return, and has been introduced in Java 5.0. You can't change it to a super type (Object) or something totally unrelated (int).
 
Marcus Deviln
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
heres the reference to the question:

"Options A, C, and E are correct. Options B and D are incorrect because an override cannot change the return type unless it's a valid covariant return. Option C is a valid covariant return."

...and here is me trying to compile with method A from above:

c:\Users\Marcus\My Documents>javac SuperFoo.java
SuperFoo.java:10: doStuff(int) in Foo cannot override doStuff(int) in SuperFoo;
attempting to use incompatible return type
found : int
required: SuperFoo
int doStuff(int x) { return 42; }
^
1 error

c:\Users\Marcus\My Documents>

Notice any inconsistencies between what the authors at Sun are saying and what the compiler is saying?
 
author
Posts: 23919
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

Marcus Deviln wrote:
...and here is me trying to compile with method A from above:

c:\Users\Marcus\My Documents>javac SuperFoo.java
SuperFoo.java:10: doStuff(int) in Foo cannot override doStuff(int) in SuperFoo;
attempting to use incompatible return type
found : int
required: SuperFoo
int doStuff(int x) { return 42; }
^
1 error

Notice any inconsistencies between what the authors at Sun are saying and what the compiler is saying?



"Method A from above" is a method that takes no parameters. If you are trying to compile method A, then why is the compiler complaining about a method that takes a single int parameter?

Henry
 
Marcus Deviln
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made a mistake above. I forgot to comment out method B before getting the above output but here is the output specifically and exclusively concerned with method A:



c:\Users\Marcus\My Documents>javac SuperFoo.java
SuperFoo.java:9: ';' expected
int doStuff() { return 42; }
^
1 error
 
Henry Wong
author
Posts: 23919
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

Marcus Deviln wrote:
c:\Users\Marcus\My Documents>javac SuperFoo.java
SuperFoo.java:9: ';' expected
int doStuff() { return 42; }
^
1 error



That's a syntax error. And it's hard to tell what is wrong without seeing the code around it too. Can you show us the source for the whole class?

Henry
 
Marcus Deviln
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should do it. Thank you very much for your help folks.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're trying to declare a method (doStuff) inside another method (main). Move the curly brace that ends the main method to before you declare doStuff, like this:

 
Marshal
Posts: 75663
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch , Steven Schwab
 
Marcus Deviln
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steven Your'e absolutely right as I'm sure is usually the case. Is this method a valid override? I thought an override had to have a return type that passed the IS-A test.
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic