Java compiler allows declaring n number of functions with the same name within the same class, as long as the parameter declarations are different. In this case, methods are said to be overloaded and this process is referred to as method overloading in java. Method overloading in java is one of the ways to implement polymorphism.
Suppose, you have to find the area of a circle, square, and triangle. You can write 3 methods with the same name and different parameters. For triangle area(int, int), for square area(int), for circle area(float). It is more useful while implementing polymorphism.
Compile-time polymorphism and run time polymorphism are the two types of polymorphism in java. Compile-time polymorphism’s example is method overloading and method overriding is an example of run time polymorphism. Java programming’s key concepts are method overloading and overriding in java
Table of Contents
How to use method overloading in java?
Define a class, in which declare two or more methods with the same name but the parameters should be different. In those methods, perform various operations. Call the methods as per the parameter listing. Make sure that the parameter sequence is right otherwise the compiler will show an exception.
There are 3 ways to Method overloading in java
1. Different number of parameters
Example
add(int, int)
add(int, int, int)
2. Sequence of parameters
Example
area(int, float)
area(float, int)
3. Using different types of data types
Example
subtract(int, float)
ubtract(float, double)
Let’s understand these ways deeply with the help of examples
Call the method with different number of parameters
In this example, The number of parameters passed to the function is different. At runtime, JVM makes the appropriate call to the function. This is the simple and best example of polymorphism using method overloading.
Example
public class method_overloading {
int add(int a, int b) // add method with 2 parameters
{
int c;
c = a+b;
return c;
}
int add(int i, int j, int k) // add method with 3 parameters
{
int p;
p = i+j+k;
return p;
}
public static void main(String[] args) {
int result;
method_overloading obj = new method_overloading();
result = obj.add(10, 20); //method overloading
System.out.println("Addition of 2 numbers is : " + result);
result = obj.add(50, 30,40); //method overloading
System.out.println("Addition of 3 numbers is : " + result);
}
}
Output:
Addition of 2 numbers is : 30
Addition of 3 numbers is : 120
Pass different sequence of parameters to the method
In this program, the parameters are the same but the sequence of the parameters are different. That is why JVM can identify the appropriate function with the right signature.
Example
public class method_overloading
{
public void show(String name, int id) // different sequence of parameters
{
System.out.println("Student Name :"+ name);
System.out.println("Student Id :"+ id);
}
public void show(int id, String name) // different sequence of parameters
{
System.out.println("Company Id :"+ id);
System.out.println("Company Name :"+ name);
}
}
class MethodDemo8{
public static void main (String[] args)
{
method_overloading obj = new method_overloading();
obj.show("Pawan misal", 18);
obj.show(20,"TCS");
}
}
Output:
Student Name :Pawan misal
Student Id :18
Company Id :20
Company Name : TCS
Pass different types of data types
In this case, the number of parameters passed to the function are the same but the data types are different. This feature of java helps more while using polymorphism.
Example
public class method_overloading {
float area(int s1, int s2) //area method for finding square's area
{
float area;
area = (float) (s1*s2); //type casting
return area;
}
double area(double l, double b) //area method for finding rectangle's area
{
double area;
area = l*b;
return area;
}
float area(int b, float h) //area method for finding rectangle's area
{
float area;
area = (float) (0.5*b*h); //type casting
return area;
}
public static void main(String[] args) {
float result;
method_overloading obj = new method_overloading();
result = obj.area(5,5); //call to area function which calculates area of circle
System.out.println("Area of circle : "+result);
result = (float) obj.area(7.5,3.5); //call to area function which calculates area of rectangle
System.out.println("Area of rectangle : "+result);
result = (float) obj.area(5,10.5); //call to area function which calculates area of triangle
System.out.println("Area of triangle : "+result);
}
}
Output:
Area of circle : 25.0
Area of rectangle : 26.25
Area of triangle : 52.5
As the methods are declared statically of the same name with different parameter lists, at compile time the call to the function is determined as per parameter list. That’s why method overloading is a compile-time polymorphism.