What the heck is Method overloading?
I first came across the concept of method overloading when I tried to solve this HackerRank challenge a couple months ago. It fascinates me that I had never come across it (ever) in all my years programming. Maybe I zoned out during the university lecture that covered this topic? I don’t know.
Anyway, method overloading is quite a rarely used feature hence it being such a rarely explained feature. So if you’re wondering what the heck method overloading is, this post might have the answers you seek.
What is method overloading?
Method Overloading is a feature that allows a class to have two or more methods having same name.
It is also known as static polymorphism because it is a form of static binding where binding of method call to its definition happens at compile time.
Why use method overloading?
The most common compelling reason to use method overloading is that it allows consistency in notation which encourages the re-usability and readability of code.
I will give an example of standard output to demonstrate its use. Most programming languages have a function or operator that will output whatever you like. For instance, in different programming languages these operations will output what you like;
LANGUAGE OPERATION C = printf() C++ = cout<<()() C# = Console.WriteLine() Java = System.out.print() Python = print()
Lets take a look at the Java operation for standard output. The overloaded versions of this method are:
System.out.print(boolean b) System.out.print(char c) System.out.print(char s) System.out.print(double d) System.out.print(float f) System.out.print(int i) System.out.print(long l) System.out.print(Object obj) System.out.print(String s)
If method overloading were not allowed, the same functions would have to be written as:
System.out.printBoolean(boolean b) System.out.printChar(char c) System.out.printCharArray(char s) System.out.printDouble(double d) System.out.printFloat(float f) System.out.printInt(int i) System.out.printLong(long l) System.out.printObject(Object obj) System.out.printString(String s)
Can you imagine how tedious it would be to have to use a different method each time you wanted to print some output? Method overloading saves efforts and presents cleaner code.
When to use method overloading
It is very possible to overload two methods that do substantially different things, however this is bad practice. It makes implementation very difficult not only for yourself but also for any future developers looking to modify your work.
So, when is method overloading appropriate? Method overloading is generally appropriate when the two methods are semantically equivalent. In short, when methods have the same purpose but there is more than one way to start them, use method overloading.
How to use method overloading
Here are some of the different ways methods can be overloaded using examples in Java.
1. With a different number of parameters
Methods can be overloaded by using a different number of parameters. The example below overloads the add() method. The first method allows only 2 parameters while the next allows 3 and the last allows 4.
The output of the above code is shown below.
5 12 22
2. With different types of parameters
Methods can also be overloaded by using different types of parameters. The example below shows the add() method again except this time it allows addition of integers and addition of doubles.
The output of the above code is:
3. With different orders of parameters
Methods can also be overloaded by simply changing the order of the parameters as in this example below. However, this would have very little practical use in my opinion and may also be counterproductive because it may cause confusion during implementation when overloading should actually prevent that.
What do you think of this post? Leave your comments…