Java Method Overloading2 min read

Method Overloading is one of the features in a class having more than one method with the same name. It is the same as Constructor Overloading in java. The methods are differed by its input parameter that is the data-type, number of parameters, and the order of parameter list.

Note that: Method overloading is not possible by changing the return type of methods.

Now let us see the different ways of overloading methods.

There are two different ways in which the method can be overloaded:

  1. Method overloading, by changing the data type of Arguments.
  2. Method overloading, by changing the number of the argument.

1. Method overloading by changing the data type of Arguments.

In this way, the user needs to change the data-type of passing argument for two different methods of the same name and the parameter list must match with that data-type accordingly.

The following program shows the example of using Method overloading by changing the data type of Arguments in Java:

Output of Method overloading:


2. Method overloading by changing the number of the argument.

Another way is by changing the number of arguments that is the value passed when called. We can also keep the argument empty and it will recognize the method with an empty parameter list.

Following is the example method overloading by changing the number of argument in Java:

Output of Method overloading:


Method.
Method Overriding.