Results 1 to 20 of 23
Thread: Method Overloading Verification
- 11-24-2010, 04:44 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
Method Overloading Verification
Hi.
I want to make sure I have the correct understanding of Method Overloading.
If the methods have the same name, BUT different formal parameter lists, they are method overload.
Exs.
Method Overload
public int methodABC (int a, char b, double c)
public int methodABC (char a, int b, double c)
Not Method Overload
If they have the same method name and the same formal lists they are NOT method overload.
public int methodABC (int a, char b, double c)
public int methodABC (int a, char b, double c)
What if they had same name, & they had the same formal list/signature, but the value/return type was different
public double methodABC (int a, char b, double c)
public int methodABC (int a, char b, double c)
- 11-24-2010, 05:29 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Yes that's overloaded, assuming those methods are in the same class anyway, or one of them is in a subclass.
The not overloaded methods cannot be in the same class (can't have two methods with the same signature in the same class)...so I assume you mean the second one is in a subclass? In which case that's overriding.
And finally, a method cannot be differentiated from another method solely by its return parameter, so those two examples cannot exist together in the same class hierarchy.
- 11-24-2010, 05:54 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
Method Overloading Verification
Thanks for responding Tolls.
I think that there must be a one-to-one match between actual and formal parameters.
If there are 4 formal, then there should be 4 actual. Is that right?
- 11-24-2010, 05:55 PM #4
What do you mean by "actual" and "formal" parameters?
- 11-24-2010, 06:14 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
I was just going to ask that question...:)
An overloaded method is a method that has the same name, but different parameters. I'm pretty sure parameter count doesn't matter.
- 11-24-2010, 06:21 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
Method Overloading Verification
KevinWorkman, this is my understanding.
Formal parameters are declared in the header:
public double pow (double x, double y)
Actual parameters are in the body (or when the method is called/used):
Java Code:double x = 2.0; double y = 2.0; z = pow(x,y) or z = pow(2.0,2.0);
- 11-24-2010, 06:29 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
Method Overloading Verification
Tolls, this is what I read:
"In a method call, the number of actual parameters, together with their dataa types, must match the formal parameters in the order given. That is, actual & formal parametes have a one-to-one correspondence." (Java Programming)
I found this on Google:
"When you call a subroutine, you must provide one actual parameter for each formal parameter in the subroutine's definition." (Java Programing: Section 4.3)
lala
Does anybody know for sure?
- 11-24-2010, 06:31 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
That's got nothing to do with overloading or overriding though.
That's to do with calling a method...which is why I was confused.
- 11-24-2010, 06:34 PM #9
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
Method Overloading Verification
I don't know, Tolls. :confused:
Does anyone know if formal and actual parameters have to have a one-to-one match?
Thanks In Advance
- 11-24-2010, 06:41 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Of course they do, but that has nothing to do with overloading, except in so far as the compiler using the parameters supplied in a call to determine which overloaded method to use.
- 11-24-2010, 07:03 PM #11
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
Method Overloading Verification
Tolls, now that I re-think this, I don't think it matters if they do not match.
- 11-25-2010, 09:28 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
What?
What are you talking about now?
Calling a method? If so, then the parameters have to match.
Overloading? Well, the parameters can't match.
Overriding? Then they have to match, otherwise it's overloading.
- 11-25-2010, 09:37 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Build a wall around Donald Trump; I'll pay for it.
- 11-25-2010, 09:47 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
No, what I mean is the OP is saying something about parameter count matching...which is why I say parameter count doesn't matter. If the signature is different (whether through parameter types or number) then it's overloading.
This is what they said:
"I think that there must be a one-to-one match between actual and formal parameters."
Which is untrue for overloading. The count doesn't matter, it's the difference in signature that does.
In essence I think the OP is mixing different concepts and confusing matters.
- 11-25-2010, 10:28 AM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Oops, sorry about the confusion I may have caused; b.t.w. overloading is a bit more complicated because of widening conversions; e.g.
Java Code:public void foo(double a, int b) { ... } public void foo(int a, double b) { ... } ... foo(1.0, 2); // fine foo(1, 2.0); // also fine foo(1, 2); // ambiguous
JosBuild a wall around Donald Trump; I'll pay for it.
- 11-25-2010, 10:32 AM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Well, yes...but there's enough confusion here without bringing that lot into it...Loretta.
- 11-25-2010, 10:39 AM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 11-25-2010, 10:58 AM #18
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
FYI:
The history behind overloading was that origionally (in older languages such as c) overloading was not possible.
This had the outcome that (to bring it forward to a java example) you had to have functions of the form:
Java Code:void printInt(int x) {...} void printDouble(double x) {...}
So later languages (c++ onwards) let you give different functions the same name as long as the compiler could allways figure out which function you were trying to use. But it was put there to let you give the same name to several functions which do the same thing (eg "print" or "add").
To that end it is usually considdered really bad practise to give the same name to two functions which do different things. Some people relax this rule a little if the different functions take a different number of arguements but the functions should have the same general purpose.
I bring this up because I have seen people intentionally write code as mindless as:Java Code:public void doSomething(int x) {turnLeft(x);} public void doSomething(double x) {turnRight(x);}
----Signature ----
Please use [CODE] tags and indent correctly. It really helps when reading your code.
- 11-25-2010, 11:12 AM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
- 11-25-2010, 05:03 PM #20
Member
- Join Date
- Oct 2010
- Posts
- 80
- Rep Power
- 0
Similar Threads
-
Overloading qn
By diskhub in forum New To JavaReplies: 18Last Post: 06-07-2010, 06:19 PM -
method overloading and arrays.
By glopez09 in forum New To JavaReplies: 5Last Post: 12-08-2009, 04:02 PM -
Demonstration of both constructor and ordinary method overloading
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 08:43 PM -
Overloading?
By padutch2 in forum New To JavaReplies: 2Last Post: 12-31-2007, 04:26 AM -
Method/Operator Overloading
By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 09:33 PM
Bookmarks