0% found this document useful (0 votes)
4 views5 pages

Method Overloading in Java - GeeksforGeeks

Method Overloading in Java allows multiple methods with the same name but different parameters, enabling compile-time polymorphism. It can be achieved by changing the number of parameters, their data types, or their order. The compiler selects the appropriate method based on the arguments passed, and if no suitable method is found, a compilation error occurs.

Uploaded by

Unknown
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Method Overloading in Java - GeeksforGeeks

Method Overloading in Java allows multiple methods with the same name but different parameters, enabling compile-time polymorphism. It can be achieved by changing the number of parameters, their data types, or their order. The compiler selects the appropriate method based on the arguments passed, and if no suitable method is found, a compilation error occurs.

Uploaded by

Unknown
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2/11/26, 8:34 PM Method Overloading in Java - GeeksforGeeks

Tutorials
Search... Practice
Sign In
Jobs
Java Tutorial Advanced Java Interview Questions Exercises Examples Quizzes Projects Cheatsheet DSA in Java Java Collection

Method Overloading in Java


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.

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 when passing to
different methods.

import [Link].*;

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 = [Link](1, 2);

// Printing Product of 2 numbers


[Link](
"Product of the two integer value: " + prod1);

// Calling method to multiply 3 numbers

[Link] 1/5
2/11/26, 8:34 PM Method Overloading in Java - GeeksforGeeks
int prod2 = [Link](1, 2, 3);

// Printing product of 3 numbers


[Link](
"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.

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


[Link]([Link](1, 2, 3));
[Link]([Link](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.
[Link] 2/5
2/11/26, 8:34 PM Method Overloading in Java - GeeksforGeeks

class Student {

public void studentId(String name, int rollNo){

[Link]("Name: " + name


+ ", Roll-No: " + rollNo);
}
public void studentId(int rollNo, String name){

[Link]("Roll-No: " + rollNo


+ ", Name: " + name);
}
}

public class Geeks{

public static void main(String[] args){

Student s = new Student();


[Link]("Sweta", 1);
[Link](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.

class Demo{

public void show(int x){

[Link]("In int: " + x);

}
public void show(String s){

[Link]("In String: " + s);


}

[Link] 3/5
2/11/26, 8:34 PM Method Overloading in Java - GeeksforGeeks
public void show(byte b){

[Link]("In byte: " + b);

}
}

public class UseDemo {


public static void main(String[] args) {
Demo obj = new Demo();
[Link]((byte) 25);
[Link]("hello");
[Link](250);
[Link]('A');
// [Link](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

Comment K kartik Follow 249

Article Tags: Java Java-Overloading

Company Explore Tutorials Courses Offline Preparation


About Us POTD Programming ML and Data Centers Corner
Corporate & Communications Address: Legal Practice Languages Science Noida Interview
Privacy Problems DSA DSA and Bengaluru Corner
A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Policy Connect Web Placements Pune Aptitude
Pradesh (201305) Careers Blogs Technology Web Hyderabad Puzzles
Contact Us AI, ML & Development Kolkata GfG 160
Registered Address:
Corporate Data Science Data Science System Design
Solution DevOps

[Link] 4/5
2/11/26, 8:34 PM Method Overloading in Java - GeeksforGeeks
K 061, Tower K, Gulshan Vivante Campus 90% CS Core Programming
Apartment, Sector 137, Noida, Gautam Training Refund Subjects Languages
Buddh Nagar, Uttar Pradesh, 201305
Program on GATE DevOps &
Courses School Cloud
Subjects GATE
Software and Trending
Tools Technologies

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

[Link] 5/5

You might also like