Page 238 - CA_Blue( J )_Class10
P. 238
11 System.out.println("Area of Rectangle : "+a);
12 }
13 void area (double r)
14 {
15 double a = 3.142 * r * r;
16 System.out.println("Area of Circle : "+a);
17 }
18 public static void main()
19 {
20 overloading calculator=new overloading();
21 calculator.area(4,2);
22 calculator.area(5);
23 calculator.area(3.5);
24 }
25 }
Explanation: In the above program, three different methods are created with the same name area() which are used
to evaluate the area of a square, rectangle, or circle. Though the methods have the same name, the parameters are
different in each of them. When executing area(int), it will calculate the area of the square; when executing area (int,
int), it will calculate the area of the rectangle; and area(double), it will calculate the area of the circle, respectively.
Thus, area() is acting as an overloaded method.
10.9.1 What type of Methods does Method overloading Support?
1. Functions with same name but different types of parameters:
Example -
a. Sum(int, int) - sum of two integer parameters
b. Sum(double, double) - sum of two real numbers
2. Functions with same name but different number of parameters:
Example -
a. Area(int) - Area of a square whose side is an integer
b. Area(int, int) - Area of a rectangle whose length and breadth is integer
3. Functions with same name and same number of parameters with different sequence of parameters.
Example -
a. Show(int, float) - Will print integer first then will print float value
b. Show(float, int) - Will print float first then print integer value
236236 Touchpad Computer Applications-X

