This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Significance  of Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Significance  of Overloading" Watch "Significance  of Overloading" New topic
Author

Significance of Overloading

Kuladip Yadav
Ranch Hand

Joined: Jul 30, 2008
Posts: 162

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

Joined: Aug 18, 2008
Posts: 598

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 ]

My Blog SCJP 5 SCWCD 5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
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

Joined: Oct 02, 2003
Posts: 11256
    
  16

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();


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
wood burning stoves
 
subject: Significance of Overloading