Method Overloading in Java - Example Program
Method Overloading in Java - Example Program
When a class has more than one method having the same name but with
different parameter lists, this feature is called method overloading in Java.
In other words, when we declare multiple methods with the same name but
with different method signatures, all these methods are overloaded methods.
We refer this feature to as method overloading.
Suppose you have a vehicle. When you apply a brake in a vehicle, it stops the
vehicle. The method brake() represents the behavior of stopping the vehicle.
The car brake is applied by the leg and the cycle brake is applied by hand.
The way of operating is different, but the desired behavior is the same.
Similarly, we can also define multiple methods with the same name in a class.
If the behavior of more than one method with the same name does the same
thing on varying numbers of parameters or varying types of parameters, it is
called overloading in java.
When a class has multiple methods having the same name with the different
parameter list, the compiler differentiates one method from another method by
checking the different number of parameters or types of passed parameters.
For example:
void add(int a, int b) // There are two parameters, a and b with data type int.
void add(int a, float b) // Here, two parameters a, and b with types int and float.
The data type is distinct in both cases. So, this is a valid case of method
overloading. If we call the method add(10, 20) by passing argument values,
JVM invokes the first method. If we invoke add(10, 20.5f), JVM executes the
second method.
b. Number of Parameters
For example:
void sum(int a, int b) // Two parameters, a and b with data type int.
void sum(int a, int b, int c) // Three parameters a, b, and c with data type int.
Here, the number of parameters is different, but the data type is the same.
This is also a valid case because there is a difference in the number of
parameters passed to methods.
For example:
void sub(int a, double b)
Here, the sequence of data type of parameters is different. So, this is a valid
case of method overloading.
6. The implementation does not matter in this case. A method can have any
kind of logic.
When multiple methods are defined with the same name in a class, then JVM
observes the signature of methods. Method signature consists of method
name along with its method parameters.
If the two methods have the same name, then their method signatures will
definitely be different. For example, two human beings can have the same
name but their signatures will definitely be different.
There are the following features of method overloading in java which you
should have to keep in mind.
Method overloading is a powerful feature in Java but you should use as per
needs. It should be used when you actually need multiple methods with
different parameters, but methods do the same thing.
If multiple methods perform different tasks, don’t use the method overloading
concept. Method overloading is done in java because of the following needs:
Let’s take an example program where we will create two methods, the first
method sum() will perform addition of two numbers and second method sum()
will perform addition of three numbers by overloading concept. Look at source
code to understand better.
Program code 1:
package methodOverloading;
public class Addition
{
// Method to calculate the sum of two numbers.
// Declare an instance method sum with two parameters a and b having data type
int.
void sum(int a, int b)
{
int s = a + b;
System.out.println("Sum of two numbers: " +s);
}
// Method to calculate sum of three numbers.
// Declare an instance method sum with three parameters a,b, and c having data
type int.
// Here, the method sum() is overloaded having the same method name.
Output:
Sum of two numbers: 30
As you can see in the above program, the method sum() is overloaded based
on the number of parameters. We have declared two methods having the
same name but the different number of parameters.
The overloaded methods have calculated the sum of two numbers and three
numbers based on the passing of arguments while calling method at runtime
by JVM.
Let’s create a program where we will create two methods having the same
name but will differ in the data type of parameters.
The first method sub() will receive two integer arguments and the second
method sub() will receive two double arguments. Look at the source code of
program.
Program code 2:
package methodoverload;
public class Subtraction
{
// Declare an instance method sub with two parameters x and y having data type
int.
int sub( int x, int y)
{
int a = x - y;
System.out.println("Subtraction of two numbers: " +a);
return a;
}
// Declare an instance method sub with two parameters x and y having data type
double.
double sub(double x, double y)
{
double b = x - y;
System.out.println("Subtraction of two numbers: " +b);
return b;
}
public static void main(String[] args)
{
Subtraction s = new Subtraction();
s.sub(6, 5); // It will call the method sub() to calculate the subtraction of two int
type arguments.
s.sub(20.8, 10.9); // It will call the method sub() to calculate the subtraction of
two double type arguments.
}
Output:
Subtraction of two numbers: 1
Program code 3:
package methodOverloading;
public class Multiplication
{
void multiply(int a, double b)
{
double m1 = a * b;
System.out.println("Multiplication of two numbers: " +m1);
}
void multiply(double a, int b)
{
double m2 = a * b;
System.out.println("Multiplication of two numbers: " +m2);
}
public static void main(String[] args)
{
Multiplication m = new Multiplication();
m.multiply(10, 20.5); // It will call method multiply() to calculate the
multiplication of two arguments int and double.
m.multiply(10.5, 30); // It will call method multiply() to calculate the
multiplication of two arguments double and int.
}
}
Output:
Multiplication of two numbers: 205.0
Let’s create a program where we will perform method overloading in the child
class or subclass. We will make a relationship between the parent class and
child class by using extends keyword.
We will create one method msg() in the parent class and second method
msg() in the child class or subclass. While calling from the class ‘Test’, the
msg() will receive int argument and double argument.
Program code 4:
package methodOverloading;
public class SuperClass
{
void msg(int x, int y)
{
System.out.println("Hello Java");
}
}
public class Subclass extends SuperClass
{
void msg(double x, double y)
{
System.out.println("Welcome you in Java programming");
}
}
public class Test
{
public static void main(String[] args)
{
Subclass sc = new Subclass();
sc.msg(10, 20);
sc.msg(2.5, 3);
}
Output:
Hello Java
In Java, Method overloading cannot be done when the return type, method
name, and argument list are the same because there may occur ambiguity.
Let’s see in the program how ambiguity may occur:
Program code 5:
package methodOverloading;
public class ReturnTypeEx1
{
public int m1(int a, int b) // Duplicate method error.
{
int x = a + b;
return x;
}
public int m1(int c, int d) // Duplicate method error.
{
int y = c * d;
return y;
}
}
public class ReturntypeTest
{
public static void main(String[] args)
{
ReturnTypeEx1 obj = new ReturnTypeEx1();
int sum = obj.m1(20, 30);
System.out.println(sum);
Output:
50
Program code 6:
package methodOverloading;
public class ReturnTypeEx1
{
public int m1(int a, int b) // Return type is int.
{
int x = a + b;
return x;
}
public double m1(int c, int d) // Return type is double.
{
int y = c * d;
return y;
}
}
public class ReturntypeTest
{
public static void main(String[] args)
{
ReturnTypeEx1 obj = new ReturnTypeEx1();
int sum = obj.m1(20, 30);
System.out.println(sum);
Output:
50
The compiler again shows the duplicate method error because the compiler
checks only method signature for duplication, not the return type.
Therefore, method overloading is not possible even though their return type is
different. So, the return type does not play any role in the case of method
overloading.
Yes, we can overload the main() method in Java. A class can have many
main() methods, but JVM calls that main() method that receives string array
as an argument only.
Therefore, the execution always starts from public static void main(String[]
args) only. Let’s understand this concept better by taking an example program
based on main method.
Program code 7:
public class MainMethodOverloadingTest
{
public static void main(String[] args)
{
System.out.println("main(String[] args)");
main();
}
public static void main()
{
System.out.println("main without args");
}
public static void main(String args)
{
System.out.println("main with string args");
}
Output:
Let’ see a few cases of the valid or invalid case of method overloading in
Java.
Case 1:
void sum(int a, int b, float c)
Result: Compile-time error. Here, the argument lists are exactly the same.
Both methods are having the same number of parameters, and the same
sequence of data types of the parameters.
Case 2:
int sum(int a, int b)
Case 3:
void msg(String a, char c)
void msg(char x)
Result: Perfectly fine. A valid case of method overloading. Here, the number
of parameters is different.
Case 4:
void msg()
void msg(char x)
Case 5:
void msg(Object o, char a)
Case 6:
Object msg(String s)
String msg(String a)
Result: Duplicate method error. Argument lists are the same. Even though
return type of methods is different. Still, it is an invalid case of method
overloading. The return type of method does not play any role while
overloading a method.
}
public static int m1(int y){
return y;