You are on page 1of 7

Method Overloading in Java

If a class has more than one method with the same name but different
parameter, it is known as method overloading.

Method overloading can be considered as an example of compile time


polymorphism or static binding or early binding. It is checked by the compiler
at compile time.

The advantage of method overloading is that it increases the readability of


the program because you don't need to use different names for the same
action.

Ambiguity in overloading:- When the compiler is unable to decide which


method is to be invoked among the overloaded methods, then the compiler
shows the ambiguity errors, the compiler does not run the program.
Causes of ambiguity:-
i) Type conversion
ii) Methods with default arguments
iii) Method with pass by reference.

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

Q. Write a java program to calculate the area of triangle, rectangle and


circle using function overloading. The program should be menu driven.

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);
}

public static void main(String args[])


{
double b,h,r,l;
float b1;
int ch;
Scanner sc=new Scanner(System.in);
OverLoadDemo1 obj=new OverLoadDemo1();
do
{
System.out.println("\n\n *****Menu***** \n");
System.out.println("\n 1. Area of Circle");
System.out.println("\n 2. Area of Triangle");
System.out.println("\n 3. Area of Rectangle");
System.out.println("\n 4. Exit");
System.out.println("\n\n Enter Your Choice : ");
ch=sc.nextInt();
switch(ch)
{
case 1:
{
System.out.println("\n Enter the Radius of Circle : ");
r=sc.nextDouble();
System.out.println("\n Area of Circle : "+obj.area(r));
break;
}
case 2:
{
System.out.println("\n Enter the Base & Height of Triangle : ");
b=sc.nextDouble();
h=sc.nextDouble();
System.out.println("\n Area of Triangle : "+obj.area(b,h));
break;
}
case 3:
{
System.out.println("\n Enter the Length & Breadth of Rectangle : ");
b1=sc.nextFloat();
h=sc.nextDouble();
System.out.println("\n Area of Rectangle : "+obj.area(b1,h));
break;
}
case 4:
System.exit(0);
default:
System.out.println("\n Invalid Choice... ");
}
}while(ch!=4);
}
}
Example-3(Ambiguity in Method Overloading)

public class OverLoadDemo3


{
void fun(int a)
{
System.out.println(a);
}
void fun(float b)
{
System.out.println(b);
}
public static void main(String args[])
{
OverLoadDemo3 obj=new OverLoadDemo3();
obj.fun(10);
obj.fun(10.32);
}

}
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{

public static void main(String[] args) {


int x = 10;
int y = 20;

System.out.println("Before Swapping");
System.out.println("Value of x is :" + x);
System.out.println("Value of y is :" + y);

//swap the value


swap(x, y);
}

private static void swap(int x, int y) {


int temp = x;
x = y;
y = temp;
System.out.println("After Swapping");
System.out.println("Value of x is :" + x);
System.out.println("Value of y is :" + y);
}
}

You might also like