Method Overloading in Java (original) (raw)
Last Updated : 22 Apr, 2025
In Java, **Method Overloading allows us to define multiple methods with the same name but different parameters within a class. This difference can be in the number of parameters, the types of parameters, or the order of those parameters.
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 more better match.
**Key features of Method Overloading:
- Multiple methods can share the same name in a class when their parameter lists are different.
- Overloading is a way to increase flexibility and improve the readability of code.
- Overloading does not depend on the return type of the method, two methods cannot be overloaded by just changing the return type.
Now, let us go through a simple example to understand the concept better:
**Example: This example demonstratesmethod 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 listed below:
- Changing the Number of Parameters.
- Changing Data Types of the Arguments.
- Changing the Order of the Parameters of Methods
1. Changing the Number of Parameters
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);
}
}
`
Output
Product of the two integer value: 2 Product of the three integer value: 6
2. 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);
}
}
`
Output
Product of the three integer value: 6 Product of the three double value: 6.0
3. 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");
}
}
`
Output
Name: Sweta Roll-No: 1 Roll-No: 2 Name: Gudly
**What if the Exact Prototype Does Not Match With Arguments?
Priority-wise, the compiler takes these steps:
- Type Conversion but to a higher type(in terms of range) in the same family.
- Type conversion to the next higher family (suppose if there is no long data type available for an int data type, then it will search for the float data type).
**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)
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
Advantages of Method Overloading
The advantages of method overlaoding is listed below:
- Method overloading improves the Readability and reusability of the program.
- Method overloading reduces the complexity of the program.
- Using method overloading, programmers can perform a task efficiently and effectively.
- Using method overloading, it is possible to access methods performing related functions with slightly different arguments and types.
- Objects of a class can also be initialized in different ways using the constructors.
Disadvantages of Method Overloading
- Too many overloaded methods can make the code harder to read and maintain.
- Similar method names with different parameters can confuse developers and lead to incorrect usage.
- Improper use may lead to ambiguity in method selection, especially with type conversions.
Method Overloading vs Method Overriding
The 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) |