• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding doStuff()

 
Marcus Deviln
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A and B have different parameter types. One is an overloaded method, the other an incorrectly overridden method.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Henry Wong
author
Marshal
Pie
Posts: 21497
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21497
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
This should do it. Thank you very much for your help folks.
 
Steven Schwab
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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:

 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch , Steven Schwab
 
Marcus Deviln
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic