enthuware.ocajp.i.v7.2.962

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Guest

enthuware.ocajp.i.v7.2.962

Post 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

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocajp.i.v7.2.962

Post 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.
If you like our products and services, please help us by posting your review here.

Statics
Posts: 6
Joined: Sat Aug 10, 2013 1:54 pm
Contact:

Re: enthuware.ocajp.i.v7.2.962

Post 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?"

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocajp.i.v7.2.962

Post 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.
If you like our products and services, please help us by posting your review here.

Statics
Posts: 6
Joined: Sat Aug 10, 2013 1:54 pm
Contact:

Re: enthuware.ocajp.i.v7.2.962

Post 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.

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocajp.i.v7.2.962

Post 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.
If you like our products and services, please help us by posting your review here.

ccavin86
Posts: 1
Joined: Mon Jun 18, 2018 1:14 pm
Contact:

Re: enthuware.ocajp.i.v7.2.962

Post 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.

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocajp.i.v7.2.962

Post by admin »

This is given in Option 2 and this is not a correct option.
If you like our products and services, please help us by posting your review here.

BaxLi007
Posts: 11
Joined: Wed Jun 26, 2019 12:43 pm
Contact:

Re: enthuware.ocajp.i.v7.2.962

Post 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?

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocajp.i.v7.2.962

Post by admin »

No, it is not useful. In fact, such confusing overloads should be avoided.
If you like our products and services, please help us by posting your review here.

morarumaria1988
Posts: 5
Joined: Mon Oct 30, 2017 5:17 am
Contact:

Re: enthuware.ocajp.i.v7.2.962

Post 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?

admin
Site Admin
Posts: 9174
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.ocajp.i.v7.2.962

Post 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?
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests