Method Overloading in Java


It is a programming concept when programmer declare
two methods of same name but with diffe re nt me tho d
sig nature e.g. change in argument list or change in type of
argument

http://www.youtube.com/watch?v=8NOKcAtcuJk
                                                   @kalkanpalworld
Example

•   public class TestOverLoading            /Define class
                                             /
          {
          / Define M
           /           ethod 1
              public static int Addition(int p, int q)
                 {
                   System.out.println(“ Method 1 is called” );
                   return p + q;
                 }
          / Define M
           /           ethod 2
             public static int Addition(int p, int q, int r)
                {
                   System.out.println(“ Method 2 is called” );
                   return p + q + r;
          }



http://www.youtube.com/watch?v=8NOKcAtcuJk
                                                                 @kalkanpalworld
Code continues
    public static void main(String[] args)
      {
       int Sum1 = Addition(9, 6);             / In this case M
                                               /              ethod 1 will be called
       int Sum2 = Addition(9, 6, 4);          / In this case M
                                              /               ethod 2 will be called
       System.out.println(“ Sum1 = ” + Sum1);
       System.out.println(“ Sum2 = ” + Sum2);

        }
    }


• output of the example code
        Method 1 is called
        Sum1 = 15
        Method 2 is called
        Sum2 = 19                             http://www.youtube.com/watch?v=8NOKcAtcuJk
                                                                                       @kalkanpalworld
http://www.youtube.com/watch?v=8NOKcAtcuJk




                                       @kalkanpalworld

Overloading in java with example

  • 1.
    Method Overloading inJava It is a programming concept when programmer declare two methods of same name but with diffe re nt me tho d sig nature e.g. change in argument list or change in type of argument http://www.youtube.com/watch?v=8NOKcAtcuJk @kalkanpalworld
  • 2.
    Example • public class TestOverLoading /Define class /  { / Define M / ethod 1 public static int Addition(int p, int q) { System.out.println(“ Method 1 is called” ); return p + q; } / Define M / ethod 2 public static int Addition(int p, int q, int r) { System.out.println(“ Method 2 is called” ); return p + q + r; } http://www.youtube.com/watch?v=8NOKcAtcuJk @kalkanpalworld
  • 3.
    Code continues public static void main(String[] args) { int Sum1 = Addition(9, 6); / In this case M / ethod 1 will be called int Sum2 = Addition(9, 6, 4); / In this case M / ethod 2 will be called System.out.println(“ Sum1 = ” + Sum1); System.out.println(“ Sum2 = ” + Sum2); } } • output of the example code Method 1 is called Sum1 = 15 Method 2 is called Sum2 = 19 http://www.youtube.com/watch?v=8NOKcAtcuJk @kalkanpalworld
  • 4.