Method Overloading in Java (original) (raw)

Last Updated : 20 Jan, 2026

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

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 when 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:

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)); 
}

}

`

**Explanation:

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:

**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

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