Last Updated : 29 May, 2025
In Java, Method Overloading allows us to define multiple methods with the same name but different parameters within a class. This difference may include:
Method overloading in Java is also known as Compile-time Polymorphism, Static Polymorphism, or Early binding, because the decision about which method to call is made at compile time. When there are overloaded methods that accept both a parent type and a child type, and the provided argument could match either one, Java prefers the method that takes the more specific (child) type. This is because it offers a better match.
Key features of Method OverloadingNow, let us go through a simple example to understand the concept better:
Example: This example demonstrates method overloading by defining multiple sum() methods with different parameter types and counts.
Java
// Java program to demonstrate working of method
// overloading in Java
public class Sum {
// Overloaded sum()
// This sum takes two int parameters
public int sum(int x, int y) { return (x + y); }
// Overloaded sum()
// This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum()
// This sum takes two double parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
Different Ways of Method Overloading in Java
The different ways of method overloading in Java are mentioned below:
Method overloading can be achieved by changing the number of parameters while passing to different methods.
Example: This example demonstrates method overloading by changing the number of parameters in the multiply() method.
Java
// Java Program to Illustrate Method Overloading
// By Changing the Number of Parameters
// Importing required classes
import java.io.*;
// Class 1
// Helper class
class Product {
// Method 1
// Multiplying two integer values
public int multiply(int a, int b)
{
int prod = a * b;
return prod;
}
// Method 2
// Multiplying three integer values
public int multiply(int a, int b, int c)
{
int prod = a * b * c;
return prod;
}
}
// Class 2
// Main class
class Geeks {
// Main driver method
public static void main(String[] args)
{
// Creating object of above class inside main()
// method
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);
}
}
Product of the two integer value: 2 Product of the three integer value: 62. Changing Data Types of the Arguments
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.
Example: This example demonstrates method overloading by changing the data types of parameters in the Prod() method.
Java
// Java Program to Illustrate Method Overloading
// By Changing Data Types of the Parameters
// Importing required classes
import java.io.*;
// Class 1
// Helper class
class Product {
// Multiplying three integer values
public int Prod(int a, int b, int c)
{
int prod1 = a * b * c;
return prod1;
}
// Multiplying three double values.
public double Prod(double a, double b, double c)
{
double prod2 = a * b * c;
return prod2;
}
}
class Geeks {
public static void main(String[] args)
{
Product obj = new Product();
int prod1 = obj.Prod(1, 2, 3);
System.out.println(
"Product of the three integer value: " + prod1);
double prod2 = obj.Prod(1.0, 2.0, 3.0);
System.out.println(
"Product of the three double value: " + prod2);
}
}
Product of the three integer value: 6 Product of the three double value: 6.03. Changing the Order of the Parameters of Methods
Method overloading can also be implemented by rearranging the parameters of two or more overloaded methods. For example, if the parameters of method 1 are (String name, int roll_no) and the other method is (int roll_no, String name) but both have the same name, then these 2 methods are considered to be overloaded with different sequences of parameters.
Example: This example demonstrates method overloading by changing the order of parameters in the StudentId() method.
Java
// Java Program to Illustrate Method Overloading
// By changing the Order of the Parameters
// Importing required classes
import java.io.*;
// Class 1
// Helper class
class Student {
// Method 1
public void StudentId(String name, int roll_no)
{
System.out.println("Name: " + name + " "
+ "Roll-No: " + roll_no);
}
// Method 2
public void StudentId(int roll_no, String name)
{
// Again printing name and id of person
System.out.println("Roll-No: " + roll_no + " "
+ "Name: " + name);
}
}
// Class 2
// Main class
class Geeks {
// Main function
public static void main(String[] args)
{
// Creating object of above class
Student obj = new Student();
// Passing name and id
// Note: Reversing order
obj.StudentId("Sweta", 1);
obj.StudentId(2, "Gudly");
}
}
Name: Sweta Roll-No: 1 Roll-No: 2 Name: GudlyWhat if the Exact Prototype Does Not Match With Arguments?
If no method matches exactly, priority-wise, the compiler takes these steps:
Let's take an example to clarify the concept:
Java
// Demo Class
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);
}
}
class UseDemo {
public static void main(String[] args)
{
byte a = 25;
Demo obj = new Demo();
// it will go to
// byte argument
obj.show(a);
// String
obj.show("hello");
// Int
obj.show(250);
// Since char is
// not available, so the datatype
// higher than char in terms of
// range is int.
obj.show('A');
// String
obj.show("A");
// since float datatype
// is not available and so it's higher
// datatype, so at this step their
// will be an error.
obj.show(7.5);
}
}
Output:
./UseDemo.java:46: error: no suitable method found for show(double)Advantages
obj.show(7.5);
^
method Demo.show(int) is not applicable
(argument mismatch; possible lossy conversion from double to int)
method Demo.show(String) is not applicable
(argument mismatch; double cannot be converted to String)
method Demo.show(byte) is not applicable
(argument mismatch; possible lossy conversion from double to byte)
1 error
The advantages of method overlaoding is listed below:
Now let's know what are the key difference between method overloading and overriding.
Method Overloading vs Method OverridingThe table below demonstrates the difference between Method Overloading and Method Overriding
Feature
Method Overloading
Method Overriding
Definition
Same method name, different parameters
Same method signature, different class
Polymorphism Type
Compile-time polymorphism (Static)
Runtime polymorphism (Dynamic)
Inheritance
No inheritance involved
Involves inheritance (parent-child)
Method Overloading in Java Programming Language
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4