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 benefit of Method overloading? 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 method would only sort over a range of elements in a container, so you must write: std : : sort v.
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.
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. Thank you for reading this article guys. Gil, J. In: Johnson, R. Gosling, J. Hejlsberg, A. Kernighan, B. MacLennan, R. Melton, H. Meyer, B. Nunnally, J. Stroustrup, B. Tempero, E. An empirical study of inheritance in java software. In: Vitek, J.
ECOOP LNCS, vol. Wilson, E. Yang, H. Zipf, G. To learn more about programming and other related concepts, check out the courses on Great Learning Academy. Also, if you are preparing for Interviews, check out these Interview Questions for Java to ace it like a pro.
Remember Me! Great Learning is an ed-tech company that offers impactful and industry-relevant programs in high-growth areas. Know More. Sign in. Log into your account. Forgot your password? Password recovery. Recover your password. Beginner 2. Intermediate 2. Beginner 1.
0コメント