File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method overloading" Watch "method overloading" New topic

method overloading

jiny tee

Joined: May 30, 2001
Posts: 11
Dear all,
May i know how the overloading can help in JAVA? What is the purpose of using it??
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Jiny,
It becomes very important because it allows a programmer to use a method name more than once. In the old days (Fortran/C/etc) you would need to make up similar names for methods that performed the same thing but accepted different parameters. In java, you can now use the same name and just accept different parameters. For example, we might have a method that computes the sum of an array that looks like the following:

But what if the user only had a vector of Integers? Should we force them to convert it to an array first and then call us, or should we give them another routine that accepts vector like below.

In java, we get to use the same name and overload the method to make it easier for calling programs!

Ah, the power of Java ...
jiny tee

Joined: May 30, 2001
Posts: 11
Thanks manfred. I think i understand now.
The purpose of using method overloading in JAVA is to use the method's name more than once with different parameters but perform the same tasks. But, in others language, we can't hv same method's name. Am i right?
The is the main reason of using method overloading?
michelle hou

Joined: Jun 11, 2001
Posts: 4
I hava Overloading question.
If exam ask me, Overloading must same return type ? (True or False)
My example:
class Test {
public static void main (String args[]) {}
public void amethod(int i, String s) {}
public int amethod(byte i, char s) {return -1; } //compile ok
// public int amethod(byte i, char s) {} not compile

Denis Anoykin

Joined: Jun 11, 2001
Posts: 8
I think that right answer is 'False'
My example:
public class Test {
public void a() {}
public int a(int b) { return 0;}
public double a(double b) {return 0;}
This code is compiled.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Overloaded methods are each their own individual methods and have nothing to do with any other method other than that they have the same name. Methods are always identified by their full signature (name + parameter list) so two methods with the same name and different parameter lists are different methods and can have different return types.
public double sumOfValues(double[] myArray) ...
public int sumOfValues(int[] myArray) ...

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link:
subject: method overloading
It's not a secret anymore!