Home > Java > Method overloading in Java – Part 1

Method overloading in Java – Part 1

What is method overloading in Java?
If two or more methods in a class have same name but different parameters, it is known as method overloading. Method overloading is one of the ways through which java supports polymorphism. Method overloading can be done by changing number of arguments or by changing the data type of arguments. If two or more method have same name and same parameter list but differs in return type then they are not said to be overloaded method.

Different ways of Method overloading

1. Method overloading by changing data type of Arguments

In this example, you can see that sum() method is overloaded two times differing in data type. The first sum() method receives two integer arguments and second sum() method receives two double arguments.

Example:

 

 

2. Method overloading by changing no. of argument.

In the below example the find() method is overloaded twice. The first takes two arguments to calculate area, and the second takes three arguments to calculate area.

Example:

 

 

When an overloaded method is called java look for match between the arguments to call the method and the method’s parameters. This match need not always be exact, sometime when exact match is not found, Java automatic type conversion plays a vital role.

 

continued in the next article Method overloading in Java – Part 2

 

This Article is TAGGED in , , , , , . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">