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.
Re: question about overload
we havent been using them for programming yet. We are just learning theory.
Re: question about overload
so your saying that the TYPE does not matter, all i look for is the Parameters in the method?
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.
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,
Jos
Re: question about overload
Quote:
Originally Posted by
JosAH
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,
Jos
Im suppose to be able to figure it out without the compiler. My questions is actually what makes something overload. I believe that only the parameters determine this. Would you concur or are there other information to take into consideration as well?
Re: question about overload
Assuming I understand Jos correctly, you are correct; only the parameters determine overloading.
Re: question about overload
Quote:
Originally Posted by
tripline
int hisMethod(int x, int y) and float hisMethod(int x, int y).
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.
Code:
int var1 = hisMethod(1,2); // should call the int method
float var2 = hisMethod(1,2); // should call the float method
hisMethod(1,2);
But what about the last line? Which method should be called?
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)
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.
Code:
public void method(String a, int i)
public void method(int i, String s)
They are 2 different methods.
Re: question about overload
Thank you. I think I understand how to look to see if a method will overload without having to test it. Let me know if there is anything i should look out for.