Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Significance of Overloading

 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
One of the query about overloading in my mind.
As we know we can overload methods. In that method having same name but
different parameters OK. e.g

But we can do by taking different name of method.

So what is acual purpose of overloading ?

thanks
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kuldeep Yadav:
Hi Ranchers,
One of the query about overloading in my mind.
As we know we can overload methods. In that method having same name but
different parameters OK. e.g

But we can do by taking different name of method.

So what is acual purpose of overloading ?

thanks


Kuldeep the point is that how many methos names you will use to denote the functionality. For the simplest example take we have a method named add(int i, int s)

and this methos is overloaded for float and LONG. So now if we go to your way we have to name functions like addFloat or addLong BUT as seen that it is really simple and apt to use one name add() for all the datataypes and arguments.
[ October 15, 2008: Message edited by: Himanshu Gupta ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Gupta is right. And there is an example you can find quite easily; you can get the String contents of a message if you know its messageName, or if you know its messageId. You can pass the name or the ID and get the message returned in both cases.

Probably best to restrict overloading of methods to instances where all the methods do something similar, but with different kinds of parameter.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how good an example this is, but i've often seen code like this:

This prevents me from having to write, and the user remembering methodWithThreeParams(), methodWithTwoParams(), methodWithOneParam, and methodWithNoParams();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic