The K&B book for Java 6 states, "Overloaded methods must change the argument list"... page 109
Are you certain that this isn't an example of overriding?
I'm confused here as you can have the same argument list when overriding (which is the case here), but the return type must be a subtype (but in this case, primitives or used).
Not sure what K&B says, but, the given option is correctly set as a wrong option because it does not overload the original method (the argument list is the same).
Although irrelevant for this question, it does not correctly override the original method either because the return type float is not compatible with int. Remember, covariant return types are applicable only for Objects not primitives.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
The last answer doesn't overload source method, so how it correlates with the question "Which of the following methods correctly overload the above method?"
Statics wrote:The last answer doesn't overload source method, so how it correlates with the question "Which of the following methods correctly overload the above method?"
Why do you think it doesn't overload the given setVar method?
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Statics wrote:The last answer doesn't overload source method, so how it correlates with the question "Which of the following methods correctly overload the above method?"
Why do you think it doesn't overload the given setVar method?
HTH,
Paul.
According to Java language specification overloading requires to have the same method name, but not override-equivalent signatures. Return type is not a part of signature and I haven't found an example of overloading with different return types. I doubt now, I used to think that return type of overloading method must have the same type as overloaded one has, or at least covariant type.
Interesting thing - just realize: have no troubles with the definition of such overload (int vs Integer).
It is ok for compilator, but ambiguous usage if try to use with default casting ...
public int setVar(int a, float b, int c) {
return (int) (a + b + c);
}
public int setVar(Integer a, float b, int c) {
return (int) (a + b + c);
}
public static void main(String args[]) {
System.out.println(setVar((int) 1,2,3)); // compilling error - reference to setVar is ambiguos ....
}
int will automatically be widened to float. Widening conversion is one of the allowed conversions. This is usually explained in certification books.
Which book are you following?
If you like our products and services, please help us by posting your review here.