• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Significance of Overloading

 
Ranch Hand
Posts: 162
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
reply
    Bookmark Topic Watch Topic
  • New Topic