cresjoy cresjoy - 5 months ago 14
Java Question

Why should I ever overload methods?

I found in my book 2 examples of overloading methods, but doesen't explain clearly exactly why its useful:

package keepo;
public class Main{
public static void main(String [] args)
int newScore = calculateScore("Tim",500);
System.out.println("New Score is" + newScore);

public static int calculateScore(String playerName, int score){
System.out.println("Player" + playerName +"Has score" + score);
return score * 1000;
public static int calculateScore(int score){
System.out.println("Unnamed player scored" + score + "points");
return score * 1000;

This is pretty straightforward, but honestly it seems pretty useless to method overload here and it seems just doing it for the sake of doing it. The next example in the book does method overloading which seems a bit more useful because that program calculates feet to centimeters, and there is one method where you can put in feet and inches, and one method where you can put inches. However it still seems just as easy to make two separate methods for this.

That being said, are there any real benefits to doing this? (I read this but I am not really to satisfied it seems just as easy to make new methods)

An elementary example would be nice. It seems to me overloading is more of something just to keep track of similar things, giving a method the same name and changing the parameters looks like an easier way of tracking methods that will do very similar things.


I think if you talk about the real benefits of function/method overloading, then as you've pointed out in your question, you won't find any.

But how is it helpful? Let's consider this example.

Let's suppose that I'm making an application that finds a person by his name and I declare and define a method

public Person[] findPerson(String name)

Now we get a requirement where we've to find a person by his Date of Birth, so introduce a new method

public Person[] findPerson_byDOB(Date date)

Let's suppose this continues and we've this many methods in my application.

public Person[] findPerson(String name)
public Person[] findPerson_byDOB(Date date)
public Person[] findPerson_byAddress(Address address)
public Person[] findPerson_bySSN(SSN ssn)
public Person[] findPerson_byDepartment(Department department)
public Person[] findPerson_byMobile(Mobile mobile)

It's just one part; this can carry on when we are asked to introduce multiple parameters, like

public Person[] findPerson_byMobileAndAddress(Mobile mobile)
public Person[] findPerson_byAddressAndDepartment(Address address, Department department)
public Person[] findPerson_byDOBAndDepartment(DOB dob, Department, department)
public Person[] findPerson_byAddressAndDOB(Address address, DOB dob)

and many many more...

While this may seem a little bit exaggerated, trust me, when making an actual industry level application, we may come across a situation when we get hundreds and hundreds of methods like this, and ultimately we'll need a catalog of all these methods of what they actually do.

It is actually a nightmare when we'll have to find the name of all these methods when we would have to use it.

However, when all the parameters are different, we can give same name to the function and it really becomes very easy to remember.

public Person[] findPerson(String name)
public Person[] findPerson(Date date)
public Person[] findPerson(Address address)
public Person[] findPerson(SSN ssn)
public Person[] findPerson(Department department)
public Person[] findPerson(Mobile mobile)
public Person[] findPerson(Mobile mobile)
public Person[] findPerson(Address address, Department department)
public Person[] findPerson(DOB dob, Department, department)
public Person[] findPerson(Address address, DOB dob)

Now as David pointed out in his answer, we all know how to get String value of integer; probably we have read it somewhere.

static String.valueOf(new Integer(1));

But do you know how many more methods are there that are overloaded with the same name?

static String.valueOf(boolean b)
static String.valueOf(char c)
static String.valueOf(char[] data)
static String.valueOf(double d)
static String.valueOf(float f)
static String.valueOf(int i)
static String.valueOf(long l)
static String.valueOf(Object obj)

The benefits are that you don't have to memorize them all. You don't even have to guess because it's the same name all the way.