TECHI WORLD
Viswanathan S
MethodOverloading
• Java allows you to define multiple methods in the same class with the same
name but with different parameters.
• Function parameters can differ in terms of their
number, type, or order.
public int add(int a, int b)
{ return a + b; }
public double add(double a, double b)
{ return a + b; }
TECHI WORLD
Viswanathan S
Workingof Method Overloading
• Method Name: Overloaded methods must have the same name.
• Parameter Lists: The parameter lists of overloaded methods must
differ in at least one of the following ways:
1) The number of parameters.
2) The data types of the parameters.
3) The order of the parameters.
6.
TECHI WORLD
Viswanathan S
Example
publicclass arithmetic {
public int add (int a, int b) {
return a + b;
}
public double add (double a, double b)
{
return a + b;
}
}
class demo{
public static void main (String args[]){
artithmetic obj = new artithmetic();
int result1 = obj.add(5, 3);
double result2 = obj.add(2.5, 4.3);
System.out.println(result1);
System.out.println (result2);
}
}