Page 1 of 1

enthuware.ocajp.i.v7.2.962

Posted: Fri Nov 02, 2012 7:19 am
by Guest
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).

Btw, I'm referring to this code:

Code: Select all

public int setVar(int a, int b, float c) { ...}
public float setVar(int a, int b, float c){return c*a; }
-- Robert

Re: enthuware.ocajp.i.v7.2.962

Posted: Fri Nov 02, 2012 9:41 am
by admin
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.

Re: enthuware.ocajp.i.v7.2.962

Posted: Sat Aug 31, 2013 1:45 am
by Statics
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?"

Re: enthuware.ocajp.i.v7.2.962

Posted: Sat Aug 31, 2013 1:56 am
by admin
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.

Re: enthuware.ocajp.i.v7.2.962

Posted: Tue Sep 03, 2013 4:03 am
by Statics
admin wrote:
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.

Re: enthuware.ocajp.i.v7.2.962

Posted: Fri Sep 27, 2013 8:55 am
by admin
Just to make it clear - The overloading method has no restriction on its return type. It can return anything.

Overriding method must have the same (or covariant) return type as the overridden method.

Re: enthuware.ocajp.i.v7.2.962

Posted: Mon Jun 18, 2018 1:22 pm
by ccavin86
Firstly, great program! I'm certain this will prepare me well for the exam.

In reference to this exam question, one of the answers defined as correct also states that:

this( ... ) can only be called in a constructor and that too as a first statement

Since we are in a method and not a constructor, how can this answer be correct?

Thanks in advance.

Re: enthuware.ocajp.i.v7.2.962

Posted: Tue Jun 19, 2018 3:22 am
by admin
This is given in Option 2 and this is not a correct option.

Re: enthuware.ocajp.i.v7.2.962

Posted: Tue Jul 02, 2019 3:46 pm
by BaxLi007
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 ....
}

have this overloading any useful usage?

Re: enthuware.ocajp.i.v7.2.962

Posted: Tue Jul 02, 2019 8:39 pm
by admin
No, it is not useful. In fact, such confusing overloads should be avoided.

Re: enthuware.ocajp.i.v7.2.962

Posted: Wed Dec 25, 2019 6:02 am
by morarumaria1988
public float setVar(int a){   
return a;
}
Here variable a from return is an int, right? Shouldn't it be a float? Like the return type of the method?

Re: enthuware.ocajp.i.v7.2.962

Posted: Wed Dec 25, 2019 10:21 am
by admin
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?