Function Overloading in Java Programming
Function in Java
In this lesson, we will understand what is function overloading in Java programming and how to create them along with some examples.
What is Function Overloading in Java?
Function Overloading in Java is a process in which we declare more than one function having the same name but with different numbers of arguments. By overloading a function, we can perform more than one task using the same name.
Suppose we declare a function called sum that takes two arguments for addition and another function with the same name, sum that takes three arguments for addition. In this case, we have overloaded the function sum.
Let's see an example of how we can overload a function in Java.
Function Overloading Example 1
import java.util.Scanner;
public class Example
{
public static int max(int a, int b)
{
int m=0;
if(a>b)
{
m=a;
}
else if(b>a)
{
m=b;
}
return m;
}
// Overloading the max function to find maximum among 3 integer numbers
public static int max(int a, int b, int c)
{
int m=0;
if(a>b && a>c)
{
m=a;
}
else if(b>a && b>c)
{
m=b;
}
else if(c>a && c>b)
{
m=c;
}
return m;
}
public static void main(String args[])
{
int x,y,z;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 integer numbers");
x=sc.nextInt();
y=sc.nextInt();
System.out.println("Maximum number = " + max(x,y));
System.out.println("Enter 3 integer numbers");
x=sc.nextInt();
y=sc.nextInt();
z=sc.nextInt();
System.out.println("Maximum number = " + max(x,y,z));
}
}
Output
Enter 2 integer numbers 15 36 Maximum number = 36 Enter 3 integer numbers 84 21 79 Maximum number = 84
In the above example, we have created two functions having the same name but with a different number of arguments. The first one will receive two integer numbers and return the maximum among the two numbers. The second function will receive three integer numbers and return the maximum among the three numbers.
It's not compulsory to use the same arguments while we overload a function. We can use different types of arguments during function overloading as per our requirements. See the example given below.
Function Overloading Example 2
import java.util.Scanner;
public class Example
{
public static float sum(int a, float b)
{
float s=0;
s=a+b;
return s;
}
// Overloading the sum function to find the sum of three different types of numbers
public static double sum(int a, float b, double c)
{
double s=0;
s=a+b+c;
return s;
}
public static void main(String args[])
{
int x;
float y;
double z;
Scanner sc=new Scanner(System.in);
System.out.println("Enter one integer and one decimal numbers");
x=sc.nextInt();
y=sc.nextFloat();
System.out.println("Sum = " + sum(x,y));
System.out.println("Enter one integer and two decimal numbers");
x=sc.nextInt();
y=sc.nextFloat();
z=sc.nextDouble();
System.out.println("Sum = " + sum(x,y,z));
}
}
Output
Enter one integer and one decimal numbers 12 14.24 Sum = 26.24 Enter one integer and two decimal numbers 25 15.364 42.2562 Sum = 82.62019841308594