Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading methods,constructors. Can't seem to find the reason for doing this

 
Chris Creed
Ranch Hand
Posts: 69
BSD Eclipse IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all

Doing some boning up to get myself back into the Java programming game, and in my attempt been doing some tutorials and what not. One of the things that I've come across that never seems to be explained to me is the reason why you woudl overloading a method or constructor. Can someone give me a real life example of why this would be done or suggested?

Thanks in advance

cc
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Creed wrote:One of the things that I've come across that never seems to be explained to me is the reason why you woudl overloading a method or constructor. Can someone give me a real life example of why this would be done or suggested?



My teacher gave us a very simple example of method overloading during my early days of OO programming. She said, consider a class having a method to add which makes addition of two integers, so you define something like this,



But what I want to add two floats numbers, I cant use the same method, So having the OOP concept , method overloading, we can have a same method named add but with different signature,



Now, you can say like, we can define generic function for this, But this is simplest and basic example of method overloading !!

Ok, this my try, and you will get some beautiful explanation from my fellow Ranchers ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic