Page 464 - Computer science 868 Class 12
P. 464

Method Overloading is a programming technique which allows multiple methods of a class to have the same name, but
              different signatures. The signature can differ by the number of input parameters or type of input parameters, or both.
              The following points are to be noted in Java overloading:
              •  In Java, method overloading is not possible by changing the return type of the methods as it leads to ambiguity.

              •  Static methods can be overloaded. However, two methods having same name and parameters and only differing by
                 static keyword cannot be overloaded.

              How Overloading works

              In Java, overloaded methods are defined by changing the
              •  Number of arguments
              •  Data type of arguments
              •  Order of the arguments
              When the parameter with best match is obtained by the compiler, corresponding overloaded method is executed. The
              actual method that gets called during runtime is thus resolved at compile time, thereby avoiding runtime errors. If the
              actual parameters do not exactly match with the formal parameters then type promotion takes place.

              However, if a no match or ambiguous match occurs, then an error message is displayed.
              Let us do some programs using overloaded methods in Inheritance.


                Program 6      A base class called Triangle is defined as follows:
                               Class name                  :  Triangle
                               Method name
                               void calarea(int b, int h)   :  To find the area of a triangle using formula
                                                              base × height/2
                               A derived class TriOverload is defined as follows:
                               Class name                  :  TriOverload
                               Method Name
                               void calarea (int a, int b, int c)  :   An overloaded method to find the area of a triangle using
                                                              the formula
                                                                ss as bs c)
                                                                (
                                                                     (
                                                                          )
                                                                           (
                                                                     )
                                                                            −
                                                                       −
                                                                  −
                 1       class Triangle  // base class
                 2       { void calarea(int b,int h) // overloaded method with two int parameters
                 3         { double a=(double)(b*h)/2;
                 4           System.out.println("Base="+b+" Height="+h+" Area="+a);
                 5       }}
                 6

                 7       class TriOverload extends Triangle  // derived class
                 8       { void calarea(int a,int b,int c) // overloaded method with three int parameters

                 9         { double s=(double)(a+b+c)/2;
                10           double ar=Math.sqrt(s*(s-a)*(s-b)*(s-c));

                11           System.out.println("a="+a+" b="+b+" c="+c+" Area="+ar);



                462462  Touchpad Computer Science-XII
   459   460   461   462   463   464   465   466   467   468   469