Updated June 13, 2023
Introduction to Method Overloading in Java
The basic meaning of overloading is performing one or more functions with the same name. Java provides the facility to overload methods. Method overloading is a single class that can have multiple methods with the same name, but they should differ in signature or number of parameters and return type of the method. This method of overloading functionality benefits code readability and reusability of the program. The JVM calls a specific method from an overloaded set, depending on the number of parameters and return type. Changing only the return type of the method java runtime system does not consider method overloading. So compulsorily, methods should differ in signature and return type.
Syntax:
class Demo
{
void addition(int val ,int val)
{
}
void addition(float val, float val, float val)
{
}
void addition(double val, double val)
{
}
}
There are two possible ways to overload:
- Changing the number of arguments.
- Changing the data types.
Consider the above code snippet; class Demo contains an overloaded addition() method with an int return type and change in a number of arguments.
Examples of Method Overloading in Java
Given below are the examples of method overloading in java:
Example #1
Consider the following example for overloading by changing a number of arguments.
Code:
import java.util.*;
class Demo2
{
int addition(int a,int b)
{
int res;
res=a+b;
return res;
}
int addition(int a,int b,int c)
{
int res;
res=a+b+c;
return res;
}
int addition(int c)
{
int res;
res=c+1;
return res;
}
}
public class OverloadingDemo
{
public static void main(String[] args)
{
Demo2 obj=new Demo2();
System.out.println("Method Overloading Demo");
Scanner scr=new Scanner(System.in);
System.out.println("Enter Three Integer values for a,b,c:");
int a=scr.nextInt();
int b=scr.nextInt();
int c=scr.nextInt();
System.out.println("Addition Method with two parameters:"+obj.addition(a, b));
System.out.println("Addition Method with Three Parameters:"+obj.addition(a, b, c));
System.out.println("Addition Method with one parameter:"+a+"+1:"+obj.addition(a));
}
}
Output:
The above example program declared three addition() methods in a Demo2 class. All three methods overload with different parameters; the first method takes three integer-type parameters and returns an integer value. The second overloaded addition() takes two integer values, calculates addition, and returns the result of an integer type value.
Example #2
Program for overloading by changing data types and return types.
Code:
import java.util.*;
class Demo3
{
float addition(int a,float b)
{
float res;
res=a+b;
return res;
}
float addition(float a,float b,float c)
{
float res;
res=a+b+c;
return res;
}
double addition(double a,double b)
{
double res;
res=a+b;
return res;
}
}
public class OverloadingDemo1
{
public static void main(String[] args)
{
System.out.println();
Demo3 obj=new Demo3();
Scanner scr=new Scanner(System.in);
System.out.println("Enter one Integer and floating point number:");
int a=scr.nextInt();
float b=scr.nextFloat();
System.out.println("Calling addition method with addition(int,float)"+obj.addition(a, b));
System.out.println("Enter three Floating point numbers:");
float i=scr.nextFloat();
float j=scr.nextFloat();
float k=scr.nextFloat();
System.out.println("Calling addition(float,float,float)"+obj.addition(i, j, k));
System.out.println("Enter two double type numbers:");
double x=scr.nextDouble();
double y=scr.nextDouble();
System.out.println("Calling addition(double,double):"+obj.addition(x, y));
}
}
Output:
The above example program demonstrates overloading methods by changing data types and return types. We declare three additional methods with differences in the type of parameters and return types. The first method takes two parameters of type int and floats to perform addition and return a float value. The second overloaded method takes three floating-point parameters to perform addition and returns a float value. And the third overloaded method takes two double values and returns a double value.
Example #3
Program to calculate the area of Square, Rectangle, and circle by overloading area() method.
Code:
import java.util.*;
class ComputeArea
{
void area(double r)
{
double area;
area=142*r*r;
System.out.println("Area of Circle for radius:"+r+":"+area+" SQ units");
}
void area(float side)
{
double res;
res=Math.pow(side,2);
System.out.println("Area of Square="+res+" Sq units");
}
void area(float len,float wid)
{
double res=len*wid;
System.out.println("Area of Rectangle (Len X Wid):"+res+" Sq units’");
}
}
public class AreaDemo
{
public static void main(String[] args)
{
Scanner scr=new Scanner(System.in);
ComputeArea obj=new ComputeArea();
System.out.println("Please Enter radius value to calculate area of circle");
double r=scr.nextDouble();
obj.area(r);
System.out.println("Please Enter side value to calculate area of square");
float x=scr.nextFloat();
obj.area(x);
System.out.println("Enter Length and width of rectangle:");
float l=scr.nextFloat();
float w=scr.nextFloat();
obj.area(l, w);
}
}
Output:
Features of Method Overloading
Given below are the features mentioned:
- Since the method overloading is static polymorphism, i.e., methods are bound during the compilation process.
- Overloading affects the runtime; methods bind at compile-time, eliminating the need for binding or checking processes during runtime.
- Only changing the return of the method does not consider method overloading. This results in an ambiguity error.
- The number of arguments, order of arguments, and type of arguments are part of the actual method overloading.
Advantages of Method Overloading
Given below are the advantages mentioned:
- The reduction in execution time occurs because the binding takes place during compilation time.
- An overloading mechanism achieves flexibility.
- Code reusability is achieved; hence it saves memory.
- Code complexity is reduced. Hence provides consistency of code.
Conclusion
Using the method overloading mechanism achieves static polymorphism. Method overloading reduces code complexity and prevents writing different methods for the same functionality with a different signature. We achieve code reusability with overloading, allowing us to use the same method for different functions. Overloading is also applied with constructors in java, but this functionality is an example of runtime polymorphism. Static polymorphism reduces execution time. It is the best programming practice to use an overloading mechanism.
Recommended Articles
This is a guide to Method Overloading in Java. Here we discuss the introduction, examples, features, and advantages of method overloading in java. You may also have a look at the following articles to learn more –