Thursday, February 16, 2023

What is the real use of Method overloading in Java and Object Oriented Programming? Example

Many programmers, including Java and C++ developer, knows about the object oriented programming concepts like 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 enough. You won't be able to reap all benefits if you just know the concept and never use that in practice. If you are wondering what is the real use of method overloading then don't worry, I will share the answer in this article. Btw, this is also one of the popular object oriented programming interview question and its good to know about it. 


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 types like 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.



What is the real use 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 languages.

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 becomes 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;
a.push_back(10);
a.push_back(20);
std::sort(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 mean, 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
template
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, like the Collection framework, is much better to use than C++ Standard template library.

What is the Use of Method Overloading in Java and Object Oriented 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)
  • 50+ OOP Interview Questions with answers (OOP questions)
  • 10 Tips to create a maintainable Java application? (tips)
  • 10 Object-Oriented design principles 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)
  • 21 tech Skills Java Programmers Can Learn (skills)
  • My Favorite Java Programming Courses for Beginners (Java courses)
  • 5 Best Courses to learn Object Oriented Programming (OOP courses)
  • My Favorite Java Design Pattern Courses (design pattern courses)
  • 5 Essential Skills to Crack Coding Interviews (skills)
  • My Favorite Courses to learn Spring MVC for Beginners (spring MVC course)
  • 5 Best Hibernate and JPA Courses for Beginners (Hibernate courses)
Thank you for reading this article guys. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note. 

No comments :

Post a Comment