• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method overloading

 
jiny tee
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
May i know how the overloading can help in JAVA? What is the purpose of using it??
Regards,
Jiny
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
Regards,
Manfred.
 
jiny tee
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) ...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic