The following code shows an Example of Method Overloading in Java.

Demonstrating an Example of Method Overloading in Java

Basically, the method overloading allows us to create several methods having the same name in a class. However, the method signatures must be different. In other words, it is possible to create several methods with the same method name as long as any one of the following is different in each method: number of parameters, type of parameters, and the order of parameters. However, the return type is not considered in method signature.

In fact, method overloading is a way to implement the compile-time polymorphism. So, the compiler binds a method call to method definition by looking at its signature.

The following code shows that we define a class Compute. Also, the class contains three methods. Furthermore, all of these methods have the same name findArea(). However, the parameters are different in each method. Hence, these three methods are performing a different task. Each one is computing the area of a different shape. Therefore, we need to call each method by providing appropriate arguments.

// Create a simple program to demonstrate method overloading
import java.io.*;
class Compute
{
    public void findArea(int len, int brd)
    {
        System.out.println("\nArea of the rectangle comes out to be "+(len*brd)+" Units!");
    }
    public void findArea(int side)
    {
        System.out.println("\nArea of the square comes out to be "+(side*side)+" Units!");
    }
    public void findArea(int first, int second, int third)
    {
        double s=(first+second+third)/2;
        double area=Math.sqrt(s*(s-first)*(s-second)*(s-third));
        System.out.println("\nArea of the triangle comes out to be "+area+" Units!");
    }
}
public class Main
{
	public static void main(String[] args) throws IOException {
		Compute rectOb=new Compute();
        System.out.println("Enter the length of rectangle...");
        BufferedReader mybuffer=new BufferedReader(new InputStreamReader(System.in));
        int length=Integer.parseInt(mybuffer.readLine());
        System.out.println("\nEnter the breadth of rectangle...");
        mybuffer=new BufferedReader(new InputStreamReader(System.in));
        int breadth=Integer.parseInt(mybuffer.readLine());
		rectOb.findArea(length, breadth);
		
		System.out.println("Enter the side of square...");
        mybuffer=new BufferedReader(new InputStreamReader(System.in));
        int side=Integer.parseInt(mybuffer.readLine());
        rectOb.findArea(side);
        
        System.out.println("Enter the first side of triangle...");
        mybuffer=new BufferedReader(new InputStreamReader(System.in));
        int first=Integer.parseInt(mybuffer.readLine());
        System.out.println("Enter the second side of triangle...");
        mybuffer=new BufferedReader(new InputStreamReader(System.in));
        int second=Integer.parseInt(mybuffer.readLine());
        System.out.println("Enter the third side of triangle...");
        mybuffer=new BufferedReader(new InputStreamReader(System.in));
        int third=Integer.parseInt(mybuffer.readLine());
        rectOb.findArea(first, second, third);
	}
}

Output

The Console Output Showing an Example of Method Overloading in Java
The Console Output Showing an Example of Method Overloading in Java

Further Reading

Understanding Enterprise Java Beans

Java Practice Exercise