Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

method overloading

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) ...
 
reply
    Bookmark Topic Watch Topic
  • New Topic