Introduction to Function Overloading in Java
Function Overloading in Java occurs when there are functions having the same name but have different numbers of parameters passed to it, which can be different in data like int, double, float and used to return different values are computed inside the respective overloaded method. Function overloading is used to reduce complexity and increase the efficiency of the program by involving more functions that are segregated and can be used to distinguish among each other with respect to their individual functionality. Overloaded functions are related to compile-time or static polymorphism. There is also a concept of type conversion, which is basically used in overloaded functions used to calculate the conversion of type in variables.
Syntax
Overloaded functions have the same name but different types of arguments or parameters assigned to them. They can be used to calculate mathematical or logical operations within the number of assigned variables in the method. The syntax of the overloaded function can be given below, where there are up to N number of variables assigned.
public class OverloadedMethod
{
public int FunctionName(int x, int y) //Two parameters in the function
{
return (x + y); //Returns the sum of the two numbers
}
// This function takes three integer parameters
public int FunctionName(int x, int y, int z)
{
return (x + y + z);
}
// This function takes two double parameters
public double FunctionName(double x, double y)
{
return (x + y);
}//Many more such methods can be done with different number of parameters
// Code used to input the number and
public static void main(String args[])
{
FunctionName s = new FunctionName();
System.out.println(s.FunctionName(10, 20));
System.out.println(s. FunctionName(10, 20, 30));
System.out.println(s. FunctionName(10.5, 20.5));
}
}
Working of Function Overloading
Function overloading works by calling different functions having the same name, but the different number of arguments passed to it. There are many coding examples that can be shown in order to identify the benefits and disadvantages of function overloading properly.
Advantage of Function Overloading
Function overloading works with the same name. So we do not have to create methods that have the same thing as work that is done inside a respective function. The functionality not only resolves the problem of conflicting naming but also improves the readability of the program.
Examples of Function Overloading
The following examples are as given below:
Example #1
In coding example 1, we will see overloaded main(), which is used to show different outputs in the panel and show how the overloaded main() is used in programming languages and how the calling of the different functions produces different outputs, respectively.
Code:
//Java program with overloaded main()
import java.io.*;
public class Test
{
// First main method which is created
public static void main(String[] args)
{
System.out.println("Main function called");
Test.main("Everyone");
}
// First overloaded main method
public static void main(String ar1)
{
System.out.println("Hi, " + ar1);
Test.main("Hello Everyone", "Buenos dias");
}
//Second overloaded main method
public static void main(String ar1, String ar2)
{
System.out.println("Mi todo eres tu, " + ar1 + ", " + ar2);
}
}
When the main() is called, there is output which is shown significantly in the output panel as shown below. First, the print statement of the main() is called and printed. Then, by distinguishing the number of arguments, the program separates the two overloaded functions which are present in the piece of code. The main() calls the first overloaded function, while the first overloaded function calls the second overloaded function.
Output:
Example #2
In the second coding example, we are going to see functions that will perform multiplication but which will have a different number of arguments assigned to them. This will help people in developing the different functions which are meant for dealing with different arguments.
Code:
class Adder
{
static int multiply(int a,int b)
{
return a*b;
}
static int multiply(int a,int b,int c)
{
return a*b*c;}
}
class TestOverloading1
{
public static void main(String[] args)
{
System.out.println(Adder.multiply(110,110));
System.out.println(Adder.multiply(110,110,110));
}
}
Output:
Example #3
In this coding example, we are going to see functions that have the same name and the same number of arguments inside them, yet they have one functionality that serves as a difference between them. The difference in functionality that is present is the datatype which is present in the functions, one is an integer, and the other is double.
Code:
//Program to add numbers distinguishing on the datatype of parameters
class Number
{
static int add(int a, int b)//Both integer variables
{
return a+b;//Returning the sum
}
static double add(double a, double b)//Both double variables
{
return a+b;//Returning the sum
}
}
class TestOverloading2
{
public static void main(String[] args)
{
System.out.println(Number.add(1,110));
System.out.println(Number.add(12.4,18.8));
}
}
In the above program, we input two integer values when we compute the sum of two integer values. The numbers entered are 10 and 20. The output should give us the value 30, which would print the sum as it is. Also, when we enter two double values, the sum is printed, which is the second overloaded function. We enter the values of the double data type variables as 15.5 and 16.3, and then we finally get to see the result as 31.8, which is the sum. We will see the output panel of the program in the below screen.
Output:
Conclusion
In this article, we see the different functionalities and concepts of Overloaded functions, which have the same name, but the different number of arguments or the data-types are different. We also notice the advantages and the syntax as to how overloaded functions need to be carried out. In this article, the overloaded functions are mainly used to multiply and do addition, which gives a great deal of detailing to the entire programming concept.
Recommended Articles
This is a guide to Function Overloading in Java. Here we discuss the working and advantages of function overloading in java along with respective examples and outputs. You may also have a look at the following articles to learn more –