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

Why Overloading is required?

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 334
2
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 22801
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've posted another example on why overloading is such a nice feature here.
 
Joe Areeda
Ranch Hand
Posts: 334
2
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 15714
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22801
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Quick! Before anybody notices! Cover it up with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic