What is the need of Method Overloading and Overriding?

Oct 7, 2021
Reaction score

I really can't seem to understand why method overloading and overriding is needed in java?
I have read some article regarding this but not able to catch why it's needed practically?

Also alongwith that I have read this resource, but I am not clear with this topic.

Any practical example will be appreciated.


Mar 3, 2021
Reaction score
They are very different concepts, so let's take them individually.

Overloading is easy: allowing different input, or input types, to a method of the same name. For the most part, this just makes the calling code much cleaner. Take, for example, a Dumper class that can print out anything you throw at it in a sensible way. Have a Dumper.dump() take an int, float, ArrayList, HashMap, etc, gives all the calling code the same interface. This can be especially useful when used with templating, where you don't always know your input type. (The alternative would be Dumper.dumpInt, dumpFloat, dumpList, dumpHash, etc. Now you've gotta run to the documentation and hope stuff is named sensibly.)

Overriding is... also easy. If you're using objects and inheritance. The parent class defines a function and a child class overrides it and provides its own implementation. This allows a child object to act like the parent, even though it's different under the hood. It's the same as implementing an Interface. You're overriding a method defined in the interface with your implementation and this allows you to place your object anywhere they're expecting the interface. We can come up with thousands of examples from the actual API, if it would help.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question