Professional Documents
Culture Documents
If a class has more than one method with the same name but different
parameter, it is known as method overloading.
Example-1
public class OverLoadDemo
{
void sum (int a, int b)
{
System.out.println ("The sum of integer: "+(a+b));
}
void sum (double a, double b)
{
System.out.println ("The sum of double: "+(a+b));
}
void sum (int a, double b)
{
System.out.println ("The sum of int and double: "+(a+b));
}
void sum (String a, String b)
{
System.out.println ("The sum of String: "+(a+b));
}
public static void main(String args[])
{
OverLoadDemo over = new OverLoadDemo();
over.sum(20,35);
over.sum(21.3,18.7);
over.sum(17, 24.6);
over.sum("Software", " Training");
}
Example-2
import java.util.*;
public class OverLoadDemo1
{
double area(double r)
{
return(3.14 * r * r);
}
double area(double b,double h)
{
return(0.5 * b * h);
}
double area(float l,double b)
{
return (l * b);
}
}
Note:-
if we write obj.fun(10.32) then ambiguity arises.
Because:-
The function fun(10) will call the first function. The function fun(10.32)
call the second function according to our prediction. But, this does not
refer to any function as in java, all the floating point constants are
treated as double not as float. if we replace float to double ,
the program works, otherwise type conversion is required as show in
the program as obj.fun(10.32F);
*/
CALL BY VALUE:-
EXAMPLE-1
public class SwapExample{
System.out.println("Before Swapping");
System.out.println("Value of x is :" + x);
System.out.println("Value of y is :" + y);