• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

attempting to use incompatible return type

 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I have placed error text and code below.
Look at the code, it makes sense to get error, if I hvae declared both method
ie. "int doh(int i)" and "float doh(int i) {"
in the same class, but they are declared in two different classes.
I don't follow why I get this error.

thanks
siva




javac Hide.java
Hide.java:15: doh(int) in Bart cannot override doh(int) in Homer; attempting to use incompatible return type
found : float
required: int
float doh(int i) {
^
1 error

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you're expecting C++ "hiding" behavior here, but that's not how Java works -- If you inherit a method, you can overload it in a subclass. The overload does not hide the inherited method, as it does in C++. The overloading rules apply just as if the two methods were defined in the same class.
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest.
> hiding" behavior here, but that's not how Java works
Why, do you see any reason ?
thanks
siva
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why, do you see any reason ?

Do you mean "what's the rationale?" I actually never understood the rationale for the C++ way of doing things; the hiding behavior has confused countless C++ newbies (including me, long ago!) I'm pretty sure at the base of it, the hiding behavior just fell out of the Cfront implementation, and was blessed by the Standard due to established practice. It's hard to see any reason why it'd be considered useful.
The Java way is both more natural and much superior, in my opinion.
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest.
-siva
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic