File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes oveloading doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "oveloading doubt" Watch "oveloading doubt" New topic

oveloading doubt

raja reddy

Joined: Aug 12, 2008
Posts: 16
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Suppose you have an abstract class, and two concrete implementations. If you use your one method per parameter name idea, e.g. :

you need to explicitly code the logic that calls one method or the other. Replace this with:

Now you can blindly call the doStuff method and the right one will be called.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Shahnawaz Shakil
Ranch Hand

Joined: Aug 04, 2008
Posts: 57
Well overloading simply let us reuse the same method name in the class. How useful this thing is, may be a matter of perception. But when you have a huge set of API then using the same name for same type of functions make sense. For example consider a function calculateArea(). This funcion should be overloaded depending upon the fact if we are calculating the area of a circle or a rectangle or any other shapy. Is it not a nicer way that the function name is always calculateArea() because this function is anyway calculating the area no matter what the shape is?
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 445


Overloading and overriding are two ways of implementing one of the basic concept of OOPS principles namely, polymorphism. I remember studying way back that the idea behind polymorphism was the same form for different uses. The main advantage of overloading can be qualified as improving the readability. Just consider a scenario where you are coding a calculator application where you have to perform various mathematical operations on different kinds of arguments. Would it not be better to have a single add()/subtract().... that takes advantage of overloading to perform the same operation for different arguments than having multiple addInteger()/addFloat().... etc? That said I do agree it improves the readability and also feel makes the code more elegant and maintainable. I am of the opinion that given a particular requirement anyone can write the code and get it to work but the real challenge is in writing code that is readable, easy to understand and to maintain (Keep in mind that more often than not the code one writes may have to be maintained by somebody else). As you mentioned the overhead on the compiler caused by overloading, I am of the view that would be minuscule as opposed to the neatness and structure it provides.

[ August 12, 2008: Message edited by: Raj Kamal ]
raja reddy

Joined: Aug 12, 2008
Posts: 16
thanks for your reply.if you dont mind,can you please elaborate it.
raja reddy

Joined: Aug 12, 2008
Posts: 16
I dont want overloading definition.I need reason other than that.Please help?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45364
Originally posted by rajaraja reddy:
I dont want overloading definition.I need reason other than that.Please help?
You already have replies; please explain what you don't know so we can help you.
I agree. Here's the link:
subject: oveloading doubt
jQuery in Action, 3rd edition