| Author |
attempting to use incompatible return type
|
Siva kandasamy
Ranch Hand
Joined: Dec 31, 2002
Posts: 139
|
|
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
Joined: Jul 08, 2003
Posts: 24054
|
|
|
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.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Siva kandasamy
Ranch Hand
Joined: Dec 31, 2002
Posts: 139
|
|
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
Joined: Jul 08, 2003
Posts: 24054
|
|
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
Joined: Dec 31, 2002
Posts: 139
|
|
Thanks Ernest. -siva
|
 |
 |
|
|
subject: attempting to use incompatible return type
|
|
|