explain function overloading with the help of a program (easy to understand)
Akash2910:
5 points are not worth it. Need at least 15
Answers
Answered by
7
HOLA amigo....
We know, Brainly is an educational app.
However, some users use this only to get more points and get higher in ranks.
However, our main motto is to check that each user gets help in solving their questions....
And so, I'm going to answer your question now, hope you will like it...(^_^)
Function Overloading is the process by which a function or method is used by the same name but different parameter names or parameter types. Java implements Polymorphism by Function/Constructor overloading.
Suppose, you have to find out "Area" of a square, a rectangle, and a circle. You will find the Area, but you are taking 3 different method names, example Area1 for calculating the area of a square, Area2 for calculating the area of a rectangle and Area3 for calculating the area of a circle.
If you do so, you will be confused during the execution of the program. However, if you use the same name for all the three functions, i.e., if you use the name Area for all the three functions, then that won't create a problem. Look at the program below:
class Sample
{
double ar = 0.0;
public void Area (int l, int b)
{
ar = l*b;
System.out.println("Area of rectangle: " + ar);
}
public void Area (int s)
{
ar = s*s;
System.out.println("Area of square: " + ar);
}
public void area (double r)
{
ar = Math.PI * (r*r);
System.out.println("Area of circle: " + ar);
}
}
Look at the above program segment.
I have used a global variable ar to calculate and print area of rectangle, square and circle.
Name of all three methods are Area, but their parameter names and types are different.
In the first method, to calculate the Area of rectangle, we have used two integer variables, l (length) and b (breadth).
In the second method, to calculate the Area of square, we have used one integer variable, s (side).
In the third method, we have used one double variable r (radius) to calculate the area of circle.
But names of all methods are the same (Area).
Hope my answer is satisfactory.....
Thanks....(^_^)
We know, Brainly is an educational app.
However, some users use this only to get more points and get higher in ranks.
However, our main motto is to check that each user gets help in solving their questions....
And so, I'm going to answer your question now, hope you will like it...(^_^)
Function Overloading is the process by which a function or method is used by the same name but different parameter names or parameter types. Java implements Polymorphism by Function/Constructor overloading.
Suppose, you have to find out "Area" of a square, a rectangle, and a circle. You will find the Area, but you are taking 3 different method names, example Area1 for calculating the area of a square, Area2 for calculating the area of a rectangle and Area3 for calculating the area of a circle.
If you do so, you will be confused during the execution of the program. However, if you use the same name for all the three functions, i.e., if you use the name Area for all the three functions, then that won't create a problem. Look at the program below:
class Sample
{
double ar = 0.0;
public void Area (int l, int b)
{
ar = l*b;
System.out.println("Area of rectangle: " + ar);
}
public void Area (int s)
{
ar = s*s;
System.out.println("Area of square: " + ar);
}
public void area (double r)
{
ar = Math.PI * (r*r);
System.out.println("Area of circle: " + ar);
}
}
Look at the above program segment.
I have used a global variable ar to calculate and print area of rectangle, square and circle.
Name of all three methods are Area, but their parameter names and types are different.
In the first method, to calculate the Area of rectangle, we have used two integer variables, l (length) and b (breadth).
In the second method, to calculate the Area of square, we have used one integer variable, s (side).
In the third method, we have used one double variable r (radius) to calculate the area of circle.
But names of all methods are the same (Area).
Hope my answer is satisfactory.....
Thanks....(^_^)
Similar questions