Open In App

Method Overloading in Java

Last Updated : 15 Oct, 2025
Comments
Improve
Suggest changes
248 Likes
Like
Report

In Java, Method Overloading allows a class to have multiple methods with the same name but different parameters, enabling compile-time (static) polymorphism.

  • Methods can share the same name if their parameter lists differ.
  • Cannot overload by return type alone; parameters must differ.
  • The compiler chooses the most specific match when multiple methods could apply.

The different ways of method overloading in Java are mentioned below:

1. Changing the Number of Parameters

Method overloading can be achieved by changing the number of parameters while passing to different methods.

Java
import java.io.*;

class Product{
    
    // Multiplying two integer values
    public int multiply(int a, int b){
        
        int prod = a * b;
        return prod;
    }

    // Multiplying three integer values
    public int multiply(int a, int b, int c){
        
        int prod = a * b * c;
        return prod;
    }
}

class Geeks{
    
    public static void main(String[] args)
    {
        
        Product ob = new Product();

        // Calling method to Multiply 2 numbers
        int prod1 = ob.multiply(1, 2);

        // Printing Product of 2 numbers
        System.out.println(
            "Product of the two integer value: " + prod1);

        // Calling method to multiply 3 numbers
        int prod2 = ob.multiply(1, 2, 3);

        // Printing product of 3 numbers
        System.out.println(
            "Product of the three integer value: " + prod2);
    }
}

Output
Product of the two integer value: 2
Product of the three integer value: 6

Explanation:

  • Two methods have the same name but different number of parameters.
  • Compiler selects the correct method based on how many arguments are passed.

2. Changing Data Types of Parameters

In many cases, methods can be considered overloaded if they have the same name but have different parameter types, methods are considered to be overloaded.

Java
class Product{
    
    public int prod(int a, int b, int c){
        return a * b * c; 
        
    }
    public double prod(double a, double b, double c){ 
        return a * b * c; 
    }
}

public class Geeks {
    public static void main(String[] args){
        
        Product p = new Product();
        System.out.println(p.prod(1, 2, 3));       
        System.out.println(p.prod(1.0, 2.0, 3.0)); 
    }
}

Output
6
6.0

Explanation:

  • Methods differ in parameter types (int vs double).
  • Compiler matches the method based on the exact data type of arguments.

3. Changing the Order of Parameters

Method overloading can also be implemented by rearranging the parameters of two or more overloaded methods.

Java
class Student {

    public void studentId(String name, int rollNo){

        System.out.println("Name: " + name
                           + ", Roll-No: " + rollNo);
    }
    public void studentId(int rollNo, String name){
        
        System.out.println("Roll-No: " + rollNo
                           + ", Name: " + name);
    }
}

public class Geeks{
    
    public static void main(String[] args){
        
        Student s = new Student();
        s.studentId("Sweta", 1);
        s.studentId(2, "Gudly");
    }
}

Output
Name: Sweta, Roll-No: 1
Roll-No: 2, Name: Gudly

Explanation:

  • Methods have the same name but parameter order is different.
  • Compiler identifies which method to call based on sequence of arguments.

Note : There can be a hybrid overloading also where number of parameters, type of parameters and order of parameters cam change in any combination.

What if the Exact Prototype Does Not Match?

Java tries type promotion in overloading

  • Convert to a higher type in the same hierarchy (e.g., byte -> int).
  • Convert to the next higher hierarchy if needed (e.g., int -> float).

If no suitable method is found, it causes a compilation error.

Java
class Demo{
    
    public void show(int x){
        
        System.out.println("In int: " + x); 
        
    }
    public void show(String s){
        
        System.out.println("In String: " + s);
        }
    public void show(byte b){ 
        
        System.out.println("In byte: " + b); 
        
    }
}

public class UseDemo {
    public static void main(String[] args) {
        Demo obj = new Demo();
        obj.show((byte) 25);  
        obj.show("hello");    
        obj.show(250);       
        obj.show('A');        
        // obj.show(7.5);     // Error: no suitable method for double
    }
}

Output
In byte: 25
In String: hello
In int: 250
In int: 65


Method Overloading in Java Programming Language
Article Tags :

Explore