0

I want to know the exact reason why the method overloading is done in OOP without using different method names to every variation as it was asked at an interview. Please help me to understand this concept.

  • This question has been asked [before](https://stackoverflow.com/questions/14228520/why-is-method-overloading-and-overriding-needed-in-java), at least in a language-specific context; however, answers tend to propose definitions and examples rather than justification. – jaco0646 Mar 12 '20 at 18:52
  • https://en.wikipedia.org/wiki/Function_overloading – epascarello Mar 12 '20 at 19:16

2 Answers2

2

Without using any fancy terms, let's say you're building an API, and there's a method called crush which let's say crushes or destroys whatever parameter is given to it. If you follow your way, you'll have to use atleast three different methods, each for an int, float and char (I'm using the most general types as an example). Now the more types there are, the more methods you'll have to create with that so many different names. Therefore the developer using your API, is going to have to remember so many different names for something as simple as a method that destroys its parameter. As much as it's difficult, it's also much less readable because again, remembering too many names for a singular function (function as in job).

Method overloading isn't used for everything, it's intended to be used for methods or functions that might take different types of data at different points, but internally follow a constant procedure or does a singular thing no matter what type of data it's passed.

You won't be writing one version of print that takes an int as a parameter, and returns the modulus of that, and another version of print that takes a string as an argument, and prints that to stdout. You can, but that's not how it's meant to be used.

1

It is mainly so as to be able to follow a relatively well-known software design principle called "Syntactic Consistency" from the book "Principles of Programming Languages" by Bruce J. MacLennan, which says the following:

Similar things should look similar, whereas

different things should look different.

When you see two functions with different names, you might be tempted to believe that they do different things. If they do in fact do different things, it is okay, but what if they do the same thing? In that case, it would be nice if the functions have the exact same name, so as to indicate that they do, in fact, do the same thing.

Of course you can misuse overloading. If you go around writing functions that do different things, taking advantage of overloading to give them the same name, then you are shooting yourself in the foot.

Community
  • 1
  • 1
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Can you link to a definition of _Syntactic Consistency_ as it relates to programming? – jaco0646 Mar 12 '20 at 18:40
  • 1
    @jaco0646 I cannot find a link that points to some good content, but I amended my answer with a reference to the book from which this principle originated. That's the best I can do. – Mike Nakis Mar 12 '20 at 19:16