Page 254 - Computer science 868 Class 12
P. 254

8.11 FUNCTION OVERLOADING
              Function overloading is a process of creating different functions with the same name having, different number or data
              types of the parameters. Function overloading is a type of Static Binding. This is because at the time of compilation, it
              is decided which function is to be called. It increases the readability of the program. There are two ways to overload
              the method in Java–either by changing the number of arguments or the data types passed as arguments.


                Program 6     Write a program using function overloading that will calculate the area of a square, rectangle
                              and circle. The name of the function is “area”.
                              1.  Area of Square: s 2
                              2.  Area of Rectangle: l * b
                              3.  Area of a Circle: 22/7*r 2

                1       class overloading
                2           {

                3               void area (int s)
                4               {

                5                   int a = s*s;
                6                   System.out.println("Area of Square : "+a);

                7               }
                8               void area (int l, int b)

                9               {
                10                  int a = l * b;

                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              void main()
                19              {

                20                 area(4,2);         // Calls area() of rectangle
                21                   area(5);         // Calls area() of square

                22                 area(3.5);         // Calls area() of circle
                23              }

                24          }







                252252  Touchpad Computer Science-XII
   249   250   251   252   253   254   255   256   257   258   259