Method Overloading sample in Java

By: Ganesh Iyer  

In same class, if name of the method remains common but the number and type of parameters are different, then it is called method overloading in Java.

class overLoading
{
   public static void main(String[] args)

  {

   functionOverload obj = new functionOverload();

   obj.add(1,2);
   obj.add(\"Life at \", \"?\");
   obj.add(11.5, 22.5);

   }
}

class functionOverload
 {
   /*  void add(int a, int b)             // 1 - A method with two parameters
     {

        int sum = a + b;
        System.out.println(\"Sum of a+b is \"+sum);

     }    */

     void add(int a, int b, int c)       {

        int sum = a + b + c;
        System.out.println(\"Sum of a+b+c is \"+sum);

     }

     void add(double a, double b)             {

        double sum = a + b;
        System.out.println(\"Sum of a+b is \"+sum);
     }

     void add(String s1, String s2)

     {
       String s = s1+s2;
       System.out.println(s);
     }
}



Archived Comments

1. //Program of Method Overloading with Runtime Values

import java.util.Scanner;

View Tutorial          By: Subhojit Das at 2014-10-09 07:31:20

2. You can not overload the private method in Test class.
there is no problem with protected met

View Tutorial          By: Gaurav at 2013-07-28 15:53:20

3. HI,

can any one tell me small but silly doubt about overloading that whether we do ov

View Tutorial          By: Amujuri Vivek at 2013-01-08 02:48:50

4. public class Overload2 {

void add(int m, int n)
{

View Tutorial          By: ITMIT at 2011-06-05 19:16:46

5. Hi.friends what is the exact purpose of "static members" in java
View Tutorial          By: SWARNA at 2010-09-13 04:25:34

6. to get more and clear information about java programming concepts.
View Tutorial          By: SWARNA at 2010-09-13 04:23:10


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial