Results 1 to 12 of 12
Thread: question about overload
- 10-27-2011, 07:48 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 19
- Rep Power
- 0
question about overload
I know this is true
int yourMethod(int x, int y) and int yourMethod(int x, double y).
but can i use different types in overload?
True or False
hisMethod(…) may be overloaded as:
int hisMethod(int x, int y) and float hisMethod(int x, int y).
the parameters are the same so it if False, but what if the types were different does it matter?
- 10-27-2011, 07:59 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: question about overload
I'm fairly sure the 2nd example you gave would not be allowed. I think that if you were to change the types of one of the variables (i.e. int hisMethod(int x,int y) and float hisMethod(int x, double y)) then that would be allowed, but I'm not entirely sure. Best thing to do would be to write a short program that uses these examples and see if they work.
- 10-27-2011, 08:04 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 19
- Rep Power
- 0
Re: question about overload
we havent been using them for programming yet. We are just learning theory.
- 10-27-2011, 08:06 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 19
- Rep Power
- 0
Re: question about overload
so your saying that the TYPE does not matter, all i look for is the Parameters in the method?
- 10-27-2011, 08:45 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: question about overload
Well, I'm not entirely sure. When I said write a program, I didn't mean make one that actually does something. Just write a couple overloaded methods using various schemes and see whether or not your IDE complains.
For example:
int foo(int x){...}
int foo(double x){...}
This one I know is okay.
int foo(int x){...}
double foo(int x){...}
This one I'm 98% sure is not okay.
int foo(int x){...}
double foo(double x){...}
This one I'm 60% sure is okay (though I would rarely, if ever, actually use this in a program because it's confusing - the return type changes based on the argument type).
Try separately putting each of these method pairs (with a "return 0;" between each pair of braces) in your Main class and see if your IDE complains.
- 10-27-2011, 09:02 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: question about overload
The compiler checks which one of the overloaded methods to use by inspecting the types of the parameter lists (and the number of parameters). If it can uniquely find one it automatically knows the type of the return value of that method.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-27-2011, 10:10 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 19
- Rep Power
- 0
Re: question about overload
- 10-28-2011, 12:17 AM #8
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: question about overload
Assuming I understand Jos correctly, you are correct; only the parameters determine overloading.
- 10-28-2011, 12:40 AM #9
Re: question about overload
The return type is not considered part of the method signature. The compiler has no idea which method to call as they have the same signature. "But what about the type of the variable the return value is being assigned to?" I hear you cry.
But what about the last line? Which method should be called?Java Code:int var1 = hisMethod(1,2); // should call the int method float var2 = hisMethod(1,2); // should call the float method hisMethod(1,2);
- 10-28-2011, 12:46 AM #10
Member
- Join Date
- Oct 2011
- Posts
- 19
- Rep Power
- 0
Re: question about overload
I was just repeating the same question hoping for a yes or no answer but i get technical responses that was hard to understand in return.
I understand what your saying now,
But in this case
int var1 = hisMethod(1,2);
float var2 = hisMethod(1,2,3);
hisMethod(1,2) //does hisMethod work or does the 3rd added index not matter because 1,2 is still the same?
what about hisMethod(1,2,3)
- 10-28-2011, 12:51 AM #11
Re: question about overload
If there is a method that takes 3 int parameters then obviously that is different to a method that takes only 2 int parameters. The order of the parameters also matters.
They are 2 different methods.Java Code:public void method(String a, int i) public void method(int i, String s)
- 10-28-2011, 05:23 AM #12
Member
- Join Date
- Oct 2011
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
Help with Square root function and Overload(calc error)
By racingoutbac in forum New To JavaReplies: 3Last Post: 11-04-2010, 12:09 AM -
OOP/RTII: How can I properly override AND overload methods for use with collections?
By CyJackX in forum New To JavaReplies: 0Last Post: 03-31-2010, 05:18 AM -
Help with this code, overload the constructor
By zoe in forum New To JavaReplies: 12Last Post: 11-03-2008, 02:55 AM -
return type determines override/overload?
By hedefalk in forum Advanced JavaReplies: 4Last Post: 07-11-2007, 01:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks