Use of Method Overloading in Java

In this class, We discuss the Use of Method Overloading in Java.

The reader should have prior knowledge of constructor overloading. Click Here.

First, We understand the concept of method overloading.

Example:

The below diagram shows the complete program.

The method add is written two times.

Writting the same method name more than once is called method overloading.

The method signature must be different.

The add method takes two inputs and three inputs.

The Java run time execution checks for signature matching during the method call.

1) Check the number of inputs matching.

ob.add(1,5) will match the two input method signature.

2) Check the type of input variable.

The below diagram shows the program.

Two add methods have the same number of inputs, but the input type differs.

One method takes integers, and the other takes double types.

ob.add(1.5, 5.5) will match the add method with double inputs.