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 benefits 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.

Btw, if you are new into the world of object-oriented programming and design then I also suggest you go through a hands-on course like UML and Object-Oriented Design Foundations by Karloy Neisztor on Udemy. It's a great course to understand the complete process of object-oriented analysis and design for creating quality software.

What is the benefit of Method overloading?

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. 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 that provides common functionalities for application programming. STL is great but it has its own set of the problem like sometimes 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 the cleanest API possible and method overloading allows you to do that without duplicating code. You can see in the above example, the real code is still in the 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 a cleaner API. It also gives you flexibility while designing the API. Since the method name is crucial to signal intent, you can use the same method until the purpose is the same. By using overloading you can use method arguments to provide additional detail like 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 a 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)

Further Learning
SOLID Principles of Object-Oriented Design
Absolute Introduction to Object-Oriented Programming in Java
Java - Object-Oriented Programming [For Absolute Beginners]

Thank you for reading this article guys. 

No comments :

Post a Comment