This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

method overloading  RSS feed

 
Clare Wright
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm struggling a bit with the reasons for method overloading, I understand why one would override a method but not overload it. Could be a stupid question...

Thanks
Cynth :roll:
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Cynth,"

It looks like you changed your display name.

Please revise your display name back to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your display name here. Thank you for your prompt attention!
-Marc
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you ever tried to create a new String? There are about 10 different ways to do it:

String()
String(byte[] bytes)
String(byte[] bytes,String encoding)
String(char[] chars)
String(StringBuffer buff)

See how useful it is? You have different choices depending on your interests and design.

Have your tried to convert a number into a String?

String.valueOf(double d)
String.valueOf(float f)
String.valueOf(long l)
String.valueOf(int d)

See how useful it is? now you can simply invoke the same method, irrespective of the type of the parameter. It would be much more difficult if you have a method with a different name for every type.

Overloading is just a way to do the same thing but with different types of parameters.
[ October 18, 2006: Message edited by: Edwin Dalorzo ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, method overloading gives the appearance of being able to pass different arguments to a method. But in fact, these are distinctly different methods.

For example, in java.util.Arrays, the binarySearch method is heavily overloaded to accept a variety of parameters...
  • binarySearch(byte[] a, byte key)
  • binarySearch(char[] a, char key)
  • binarySearch(double[] a, double key)
  • etc...
  • With overloading, a user is able to call by the same name whichever is most convenient or appropriate. Without overloading, we would need to know the different names of each version. For example, something like...
  • binarySearchByteArray(byte[] a, byte key)
  • binarySearchCharArray(char[] a, char key)
  • binarySearchDoubleArray(double[] a, double key)
  • etc...
  •  
    Clare Wright
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oops sorry about that Marc, got married hence the profile change. Thank you both so much for the help, I'm getting it now!
     
    shruti kamath
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks very good example in terms of both constructor as well as method overloading

    Edwin Dalorzo wrote:Have you ever tried to create a new String? There are about 10 different ways to do it:

    String()
    String(byte[] bytes)
    String(byte[] bytes,String encoding)
    String(char[] chars)
    String(StringBuffer buff)

    See how useful it is? You have different choices depending on your interests and design.

    Have your tried to convert a number into a String?

    String.valueOf(double d)
    String.valueOf(float f)
    String.valueOf(long l)
    String.valueOf(int d)

    See how useful it is? now you can simply invoke the same method, irrespective of the type of the parameter. It would be much more difficult if you have a method with a different name for every type.

    Overloading is just a way to do the same thing but with different types of parameters.
    [ October 18, 2006: Message edited by: Edwin Dalorzo ]
     
    Campbell Ritchie
    Marshal
    Posts: 55741
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the Ranch
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!