Demonstrate the method overloading concept

Q. Write a program to demonstrate the method overloading concept in Java.

Answer:

Method Overloading:
A class has more than one methods has same name, but different argument known as method overloading.

In this example, the OverloadTest class have more than one sum() method. Each method performs the addition operation on different datatype.

OverloadTest.java

class OverloadTest
{
    void sum (int a, int b)
    {
        System.out.println ("The sum of integer: "+(a+b));
    }
    void sum (double a, double b)
    {
        System.out.println ("The sum of double: "+(a+b));
    }
    void sum (int a, double b)
    {
        System.out.println ("The sum of int and double: "+(a+b));
    }
    void sum (String a, String b)
    {
        System.out.println ("The sum of String: "+(a+b));
    }
    public static void main(String args[])
    {
        OverloadTest over = new OverloadTest();
        over.sum(20,35);
        over.sum(21.3,18.7);
        over.sum(17, 24.6);
        over.sum("TutorialRide", " Info");
    }
}


Output:

method overloading