1. Home
  2. Java
  3. Java OOPs
  4. Method Overloading

Method Overloading

What is Method Overloading?

Using multiple methods with the same name and different parameters is known as method overloading in Java. You can create multiple methods with the same name in a Java program, but all these methods differ with the number of parameters or data types. This is called Method Overloading in Java.

Method Overloading in Java

The parameters for overloading functions may differ with three types:

1. Number of parameters –

2. Datatype of Parameters –

3. Sequence of Datetype of parameters –

Running Example of Method Overloading

Here is a simple example of method overloading in Java. A class named CalculateSum is created with 3 functions with the same name “sum” but different parameters. See below example:

Explanation:-

  • First sum() function accepts two parameters of type int.
  • Second sum() function accepts 2 parameters, one of type int and one of type double.
  • The third sum() function accepts 3 parameters, where one 2 parameters of type int and one double.
  • The main() function call sum 3 times with different-2 parameters. Here method overloading is applied. It executes that function with matches similar to parameters passed during the function call.

Run Java Program:-

Now save above script in CalculateSum.java file. Then compile and run this program on the command line.

javac CalculateSum.java
java CalculateSum
Results
15
30.5
60.5
Tags , ,