This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why Overloading is required? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why Overloading is required?" Watch "Why Overloading is required?" New topic
Author

Why Overloading is required?

Varun Nayudu
Ranch Hand

Joined: Jun 26, 2006
Posts: 157
Hi,
I want to ask this simple question

1>What is the need for overloading method?
2> Why cant we create a different method instead of overloading the method?


SCJP 1.5, SCWCD 1.5
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 294
    
    2

Varun Nayudu wrote:Hi,
I want to ask this simple question

1>What is the need for overloading method?
2> Why cant we create a different method instead of overloading the method?
Well, required is a bit strong but overloading is one the more powerful and useful features of Object Oriented programing.

For example if we have a base class of People with an abstract method called beNice() and 2 derived classes Boys and Girls. The calling routine only needs to know about People and always calls beNice(). If the People happens to be a Girl it expresses it's feelings for hours and watches bad movies with them, if it's a Boy it brings beer. If you had separate methods you'd have to a) know about all the derived classes and b) determine which method to call

Now if later on someones adds Dogs (dogs are people too) all they have to do is implement the abstract methods, they don't have to go find all the places beNice() is called and add another test of what it is and call the appropriate method those places still call beNice() and the dog gets a treat and a pat on the head.

Hope that helps and the example isn't too silly.

Joe


It's not what your program can do, it's what your users do with the program.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

Joe Areeda wrote:
Varun Nayudu wrote:Hi,
I want to ask this simple question

1>What is the need for overloading method?
2> Why cant we create a different method instead of overloading the method?
Well, required is a bit strong but overloading is one the more powerful and useful features of Object Oriented programing.

For example if we have a base class of People with an abstract method called beNice() and 2 derived classes Boys and Girls. The calling routine only needs to know about People and always calls beNice(). If the People happens to be a Girl it expresses it's feelings for hours and watches bad movies with them, if it's a Boy it brings beer. If you had separate methods you'd have to a) know about all the derived classes and b) determine which method to call

Now if later on someones adds Dogs (dogs are people too) all they have to do is implement the abstract methods, they don't have to go find all the places beNice() is called and add another test of what it is and call the appropriate method those places still call beNice() and the dog gets a treat and a pat on the head.

Hope that helps and the example isn't too silly.

Joe


That's overriding, not overloading.


luck, db
There are no new questions, but there may be new answers.
vicky Aggarwal
Ranch Hand

Joined: May 15, 2009
Posts: 40
Hi,

I will go with a simple one.
Suppose you cerate a method sum(int i, int j), which add two number.
After a while you realize that you require a method for adding 3 numbers or 4 numbers etc.
So it will make a requirement that there is lots of methods with differentnumber of argumnets and
performing same kind of functionality on these argument.
Without overloading you have to remember the name of method which is having exact set of parameter.
But using overloading makes it simple just one name with different parameter.
int sum (int i, int j) {}
int sum (int a, int b, int c) {}

I hope it will help.

P.S. aboove example can be done using varargs, but it's just to understand overloading.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

There are quite a few good examples in the core libraries. Take System.out.println, for instance.

Without overloading, you could have a different method name for each type. printInt, printByte, printShort, printLong, printBoolean, printDouble, printFloat, printObject. And then you have to make sure you're calling the right one.

Isn't it easier if they've all got the same name, since they're doing the same thing?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

I've posted another example on why overloading is such a nice feature here.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 294
    
    2

Darryl Burke wrote:

That's overriding, not overloading.
Oops that's what happens when I start typing before I start thinking.
You're right. I apologize.
Varun Nayudu
Ranch Hand

Joined: Jun 26, 2006
Posts: 157
Thanks guys, but my question is simple, WHY

Even i know creating different methods name is dum . But there might be something more to overloading than just to keep the method name's similar so that we dont have to remember methods.

I m sorry for this. But the anwser's posted are all bookesh . I could find this by just googling. I want to know the main reason why we use overloading other than just to keep the method names same for similar operation .

For example
As stated in the prevous post if you have a operation for adding 2 numbers you create a method 'sum(int a, int b)' now you want to add 3 numbers i could have created another method named 'sumthree(int a,int b, int c)' instead of overloading it. Even in overlading you would have to write the code within the method.

So my question still persists
WHY
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3361
    
    9
Varun Nayudu wrote:But there might be something more to overloading than just to keep the method name's similar so that we dont have to remember methods.


No there's not. This is the only reason. Why do you assume there's more to it?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Stephan's right, it's purely down to convenience and readability. The method name should somehow describe what the method does, so that the code is clear and it's easy to remember the methods. That gets more complicated if you have to come up with a new name every time you want to do exactly the same thing with different arguments.

If you've used a language without overloading, have you never thought "you know, I really wish I could just reuse that method name with different arguments". I have - loads of times. The people that designed Java probably had as well.

(The stuff you find by Googling is sometimes right, you know!)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Matthew Brown wrote:If you've used a language without overloading, have you never thought "you know, I really wish I could just reuse that method name with different arguments". I have - loads of times. The people that designed Java probably had as well.

Overloading isn't new to Java. Languages like C++ and Delphi also have it. Since Java is based on C++ that's probably why Java has it in the first place.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Rob Spoor wrote:
Overloading isn't new to Java. Languages like C++ and Delphi also have it. Since Java is based on C++ that's probably why Java has it in the first place.

True. I should have said "the people that designed <whatever the first language that had overloading> probably had as well".
sandy chops
Greenhorn

Joined: Nov 11, 2010
Posts: 8
Here is my take on this.
This is how java supports polymorphism using method overloading, "one interface, multiple methods paradigm'
One benefit of overloading is that you can access related methods by using common name.
Also when an overloaded method is invoked, java uses the type and/ or number of args to determine which method to actually call.
This avoids coding error if there are different methods names and a wrong method is coded by the programmer.
hope this helps.
Varun Nayudu
Ranch Hand

Joined: Jun 26, 2006
Posts: 157

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Overloading is required?
 
Similar Threads
method overriding
Is overloading Polymorphism?`
java [question -- overloading]
overlading
Dynamic and Static Polymorphism