What is the real use of Method Overloading in Java or Programming?

Many programmers, including Java and C++, knows about overloading e.g. method overloading or function overloading, but if you ask them why you should overload a method? Many of them become clueless. This is a common problem of half learning i.e. you know the concept but you don't know the application. If you neither know what problem it solves nor what benefit it provides, then just knowing the concept is not good enough. You won't be able to reap all benefit if you just know the concept and never use it in practice. The most important benefit overloading provides is a cleaner and intuitive API.

It also gives you more flexibility while writing API, for example, you can look at the println() method. The job of this method is to print something and add a new line. Now, you can print any kind of data type e.g. int, short, long, float, String or Object.

This is where overloading makes your job easy, a println() is to print and method parameters will describe what to print. If overloading was not available, you would end up with clumsy APIs like printString(), printObject(), printInteger() etc. So, method overloading provides you flexibility while writing API.

Overloading also makes your API simpler, to give you an example, I'll pick the sort() method from C++ Standard Template Library (STL) i.e. std::sort function. The C++ is a great language and provides you the right balance between low and high-level language. It comes with a rich standard template library which provides common functionalities for application programming. STL is great but it has its own set of problem e.g. sometime it become unnecessary verbose and counter-intuitive.

The std:sort() sort elements in a container in place, for example, you expect to sort elements of Vector as shown below:

vector<int> v;

But, No, it won't work like that. The std::sort method would only sort over a range of elements in a container, so you must write:

std::sort(v.begin(), v.end());

Had they used overloading and provided a sort() method, it would have been much easier to use i.e. when you want to sort all elements of Vector, you could have used that. Here is a simple example of overloading which many programmers would have appreciated and might have done by themselves

void sort(T& t) {
  return std::sort(t.begin(), t.end());

So, Overloading can make your API a lot simpler to use. As an API designer, you must provide cleanest API possible and method overloading allows you to do that without duplicating code. You can see in above example, the real code is still in original method but a wrapper around that makes the client code much simpler. This is one area where Java really beats C++. Java's API e.g. Collection framework is much better to use than C++ Standard template library.

What is the real use of Method Overloading in Java or Programming?

That's all about the real use of method overloading in programming. It helps to provide cleaner API. It also gives you flexibility while designing API. Since method name is crucial to signal intent, you can use the same method until the purpose is same. By using overloading you can use method arguments to provide additional detail e.g. sorting a list or array or a container by overloading sort() method. Method overloading also prevents duplicate code by allowing you to leverage existing methods to wrap around.

Other Programming articles you may like
  • 10 Tips to improve your programming skill and become a better Programmer? (tips)
  • 10 Tips to create maintainable Java application? (tips)
  • 10 Object-Oriented design principle every Java programmer should know? (article)
  • What is the difference between an average and a good programmer? (article)
  • Is Java Concurrency in Practice still relevant in the era of Java 8? (opinion)
  • 5 Books to Improve Coding of Programmers (books)

Thank you for reading this article guys. Wish you a very Happy New Year 2017. 

No comments :

Post a Comment