What is a function overloading ?
Function Overloading is a feature that allows a class to have more than one function having the same name, if their lists of arguments are different.
For example, the argument list of a method multiple(int x, int y) having two parameters is different from the argument list of the method multiple(int x, int y, int z) having three parameters.
There are two ways to do function overloading :-
By changing the number of arguments
Example :-
In this example we are declaring two methods with the same but the number of arguments is different here,
class addition{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Addition.add(11,11));
System.out.println(Addition.add(11,11,11));
}}
Output :-
22
33
By changing arguments data types
Example :-
In this example we are declaring two methods with the same but the argument data type is different here,
class Adder{
static int add(int a, int b){return a+b;}
static double add(double, double b){return a+b;}
}
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}
Output :-
22
24.9