give example of method overloading in java
Answers
Answered by
1
Overloading – Different Number of parameters in argument list
This example shows how method overloading is done by having different number of parameters
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
Output:
a
a 10
In the above example – method disp() is overloaded based on the number of parameters – We have two methods with the name disp but the parameters they have are different. Both are having different number of parameters.
This example shows how method overloading is done by having different number of parameters
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
Output:
a
a 10
In the above example – method disp() is overloaded based on the number of parameters – We have two methods with the name disp but the parameters they have are different. Both are having different number of parameters.
Answered by
0
Using overloading write a program to calculate and print the area of a square, rectangle and circle.
class AreaOverload
{
double a;
void area(int side)
{
a=side*side;
System.out.println("Area of a square is :"+a);
}
void area(int len, int br)
{
a=len*br;
System.out.println("Area of a Rectangle is :"+a);
}
void area(double r)
{
a=3.14*r*r;
System.out.println("Area of a circle is :"+a);
}
public static void main(String args[ ] )
{
AreaOverload ar=new AreaOverload();
ar.area(3);
ar.area(4,6);
ar.area(3.5);
}
}
Similar questions