File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes  regarding doStuff() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " regarding doStuff()" Watch " regarding doStuff()" New topic
Author

regarding doStuff()

Marcus Deviln
Ranch Hand

Joined: Jan 29, 2009
Posts: 39

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


+Deviln+ The power of JAVA compels you!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

A and B have different parameter types. One is an overloaded method, the other an incorrectly overridden method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Joined: Jan 29, 2009
Posts: 39
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
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

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


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

Joined: Jan 29, 2009
Posts: 39
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
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

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

Joined: Jan 29, 2009
Posts: 39
This should do it. Thank you very much for your help folks.
Steven Schwab
Greenhorn

Joined: May 25, 2010
Posts: 9
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

Joined: Oct 13, 2005
Posts: 38881
    
  23
Welcome to the Ranch , Steven Schwab
Marcus Deviln
Ranch Hand

Joined: Jan 29, 2009
Posts: 39
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding doStuff()