In Java, Method Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters, or a mixture of both.
Method overloading in Java is also known as Compile-time Polymorphism, Static Polymorphism, or Early binding. In Method overloading compared to the parent argument, the child argument will get the highest priority.
Example of Method Overloading
Java
public class Sum {
public int sum( int x, int y) { return (x + y); }
public int sum( int x, int y, int z)
{
return (x + y + z);
}
public double sum( double x, double y)
{
return (x + y);
}
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
- 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.
Below is the implementation of the above method:
Java
import java.io.*;
class Product {
public int multiply( int a, int b)
{
int prod = a * b;
return prod;
}
public int multiply( int a, int b, int c)
{
int prod = a * b * c;
return prod;
}
}
class GFG {
public static void main(String[] args)
{
Product ob = new Product();
int prod1 = ob.multiply( 1 , 2 );
System.out.println(
"Product of the two integer value :" + prod1);
int prod2 = ob.multiply( 1 , 2 , 3 );
System.out.println(
"Product of the three integer value :" + prod2);
}
}
|
OutputProduct 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.
Below is the implementation of the above method:
Java
import java.io.*;
class Product {
public int Prod( int a, int b, int c)
{
int prod1 = a * b * c;
return prod1;
}
public double Prod( double a, double b, double c)
{
double prod2 = a * b * c;
return prod2;
}
}
class GFG {
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);
}
}
|
OutputProduct 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.
Below is the implementation of the above method:
Java
import java.io.*;
class Student {
public void StudentId(String name, int roll_no)
{
System.out.println( "Name :" + name + " "
+ "Roll-No :" + roll_no);
}
public void StudentId( int roll_no, String name)
{
System.out.println( "Roll-No :" + roll_no + " "
+ "Name :" + name);
}
}
class GFG {
public static void main(String[] args)
{
Student obj = new Student();
obj.StudentId( "Spyd3r" , 1 );
obj.StudentId( 2 , "Kamlesh" );
}
}
|
OutputName :Spyd3r Roll-No :1
Roll-No :2 Name :Kamlesh
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
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();
obj.show(a);
obj.show( "hello" );
obj.show( 250 );
obj.show( 'A' );
obj.show( "A" );
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
- 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.
Important Questions in Java
Q1. Can we overload static methods?
Answer:
The answer is ‘Yes’. We can have two or more static methods with the same name, but differences in input parameters. For example, consider the following Java program. To know more about this, refer to the article – Can we Overload or Override static methods in Java?
Q2. Can we overload methods that differ only by static keywords?
Answer:
We cannot overload two methods in Java if they differ only by static keyword (the number of parameters and types of parameters are the same). See the following Java program for example. Refer to this for details.
Q3. Can we overload main() in Java?
Answer:
Like other static methods, we can overload main() in Java.
Java
import java.io.*;
public class Test {
public static void main(String[] args)
{
System.out.println( "Hi Geek (from main)" );
Test.main( "Geek" );
}
public static void main(String arg1)
{
System.out.println( "Hi, " + arg1);
Test.main( "Dear Geek" , "My Geek" );
}
public static void main(String arg1, String arg2)
{
System.out.println( "Hi, " + arg1 + ", " + arg2);
}
}
|
OutputHi Geek (from main)
Hi, Geek
Hi, Dear Geek, My Geek
Q4. Does Java support Operator Overloading?
Answer:
Unlike C++, Java doesn’t allow user-defined overloaded operators. Internally Java overloads operators, for example, + is overloaded for concatenation.
Q5. Can we overload methods on return type?
Answer:
We cannot overload by return type. This behavior is the same in C++. Refer to this for details.
Java
import java.io.*;
public class Main {
public int foo() { return 10 ; }
public char foo() { return 'a' ; }
public static void main(String args[]) {}
}
|
Error
./Main.java:8: error: method foo() is already defined in class Main
public char foo() { return 'a'; }
^
1 error
However, Overloading methods on return type are possible in cases where the data type of the function being called is explicitly specified. Look at the examples below:
Java
import java.io.*;
public class Main {
public static int foo( int a) { return 10 ; }
public static char foo( int a, int b) { return 'a' ; }
public static void main(String args[])
{
System.out.println(foo( 1 ));
System.out.println(foo( 1 , 2 ));
}
}
|
Java
class A {
public int foo( int a) { return 10 ; }
public char foo( int a, int b) { return 'a' ; }
}
public class Main {
public static void main(String args[])
{
A a = new A();
System.out.println(a.foo( 1 ));
System.out.println(a.foo( 1 , 2 ));
}
}
|
Q6. What is the difference between Overloading and Overriding?
Answer:
Overloading is about the same function having different signatures. Overriding is about the same function, and same signature but different classes connected through inheritance.

Overloading is an example of compiler time polymorphism and overriding is an example of run time polymorphism.
Related Articles