Page 258 - ComputerScience_Class_11
P. 258

The differences between pure and impure method are as follows:
                                      Pure Method                                 Impure Method
                      There is no change in the state of an object.  There is a change in the state of an object.
                      A pure method is a returnable method.        An impure method may or may not return a value.

                      It is also known as Accessor.                It is also known as Mutator.

                   9.8 METHOD OVERLOADING
              Sometimes, more than one method may have the same name. But this may lead to confusion for the compiler about
              the  function to be called. So, to counter this problem, there should be some difference in their arguments. So, these
              methods (with the same names) should have either.
              •  Different type of parameters, or
              •  Different number of parameters
              Let us consider the following program.

                Program 8     Write a program that calculates area of circle, square and rectangle.

                1       class FunctionOverloading
                2       {
                3           void area(double r)
                4           {
                5               double ar = 3.142 * r * r;

                6               System.out.println("The Area of a Circle: " + ar);
                7           }
                8           void area(int s)
                9           {

                10              int ar = s * s;
                11              System.out.println("The Area of a Square: " + ar);
                12          }
                13          void area(int l, int b)
                14
                            {
                15              int ar = l * b;
                16              System.out.println("The Area of a Rectangle: " + ar);
                17          }
                18          public static void main(String[] args)
                19          {

                20              FunctionOverloading obj = new FunctionOverloading();
                21              obj.area(5.6);
                22              obj.area(5, 2);
                23              obj.area(5);
                24          }

                25      }





                  256  Touchpad Computer Science (Ver. 3.0)-XI
   253   254   255   256   257   258   259   260   261   262   263