Method Overloading in Java

Method o function Overloading in Java
Method

What is method overloading?

Ans: When we define multiple methods with the same name but with different data, types are called Method overloading. Method overloading is also called Static polymorphism because it is resolved at compile time.

Why use Method overloading?

In OOPs an object contains data and method in a single unit. Sometimes we are required to perform similar operations with different data types. If we use a different function name then it is a little bit difficult to remember. But when we define more than one method with a similar name then it is easier to remember and is also reliable.

How to define the Overloading method?

We define methods with the same name with different parameters is also known as a method signature. For example
class Summation
{
                int a,b,c;
                double x,y,z;
                void sum(int m, int n)
                {
                                a = m;
                                b = n;
                                c = a+b;
                                System.out.println(c);
                }
                void sum(double m, double n)
                {
                                x = m;
                                y = n;
                                z = x+y;
                                System.out.println(z);
                }


}


// Method Overloading of Area


public class AreaCalculator

{

   int s,l, b, ar1;

   double r, ar2;

   void area(int x)

   {

       s=x;

       ar1=s*s;

       System.out.println("Area Of Square:"+ar1);

    }

    void area(int m, int n)

    {

        l=m;

        b=n;

        ar1=l*b;

        System.out.println("Area Of Rectangle:"+ar1);

    }

    void area(double rad)

    {

        r=rad;

        ar2=3.14*r*r;

        System.out.println("Area Of Circle:"+ar2);

    }

    public static void main()

    {

        AreaCalculator ob=new AreaCalculator();

        ob.area(5,6);

        ob.area(5.0);

    }

}


Another example

/**
 * This program compares two integers, two char, and two String values
 * with the help of method overloading
 * 
*/
import java.io.*;
public class MethodOverLoading
{
    int a,b;                    // instance variable
    char a1,b1;
    String a2,b2;
    void compare(int x,int y)
    {
        a=x;
        b=y;
        if(a>b)
         System.out.println("greatest="+a);
        else
         System.out.println("greatest="+b);
    }
    void compare(char x,char y)
    {
        a1=x;
        b1=y;
        if(a1>b1)
         System.out.println("Higher value="+a1);
        else
         System.out.println("Higher Value="+b1);
    }
    void compare(String x,String y)
    {
        a2=x;
        b2=y;
        int n,m;
        n=a2.length();
        m=b2.length();
        if(n>m)
         System.out.println("Longer String="+a2);
        else
         System.out.println("Longer String="+b2);
    }
    public static void main(String args[])throws IOException
    {
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(in);
        int a,b;        // Local Variables
        char a1,b1;
        String a2,b2;
        MethodOverLoading ob=new MethodOverLoading();
        System.out.println("Enter two number");
        a=Integer.parseInt(br.readLine());
        b=Integer.parseInt(br.readLine());
        ob.compare(a,b);
        System.out.println("Enter Two Character");
        a1=(char)br.read();
        b1=(char)br.read();
        ob.compare(a1,b1);
        System.out.println("Enter two String");
        a2=br.readLine();
        b2=br.readLine();
        ob.compare(a2,b2);
    }
     
}
Output
Enter two number
4
5
greatest=5

Enter Two Character
B
A
Higher value=B

Enter two String
Hello
Bolo
Longer String=Hello



Another Example

// Overload series methods ICSE2019

public class OverloadICSE2019
{
    long sum1 = 0L;
    int i, num;
    double sum2;
    public void series(int x, int n)
    {
        
        for(i = 1; i <= n; i++)
        {
            sum1 += (long)Math.pow(x, i);
        }
        System.out.println("Sum = " + sum1);
    }
    public void series(int p)
    {
        for( i = 1; i <= p; i++)
        {
             num = i*i*i - 1;
            System.out.print(num + "\t");
        }
    }
    public void series()
    {
        sum2 = 0.0;
        for(i = 2; i <= 10; i++)
            sum2 += 1.0 / i;
        System.out.println("Sum = " + sum2);
    }
}

ICSE Computer Applications Year 2013 Question 8 solved

Design a class to overload a function series() as follows:
(i) double series(double n) with one double argument and returns the sum of the series, sum = 1 / 1 + 1 / 2 + 1 / 3 + … + 1 / n.
(ii) double series(double a, double n) with two double arguments and returns the sum of the series, sum = 1 / a2 + 4 / a5 + 7 / a8 + 10 / a11 + … to n terms.

class SeriesOverload
{
    double no, sum;
    double a, s;
    int p, count;
    public double series(double n)   // Method with single parameter
    {
        no=n;
        s=0.0;
        sum = 0.0;
        for(int i = 1; i <= no; i++)
        {
            s= s + 1.0/i;
            sum = sum + s;
        }
        return sum;
    }
    public double series(double x, double y)// The method with double parameter
    {       
        a=x;
        no=y;
        s=0.0;
        count = 1;
        p = 2;
        for(int i = 1; i <= no; i++)
        {
            s=s + count / Math.pow(a, p);
            sum = sum + s;
            count += 3;
            p += 3;
        }
        return sum;
    }
    public static void main(String args[])
    {
        double n=10, a=5, summation;
        SeriesOverload ob=new SeriesOverload();
        summation=ob.series(n);
        System.out.println("Summation="  + summation);
        summation=ob.series(n, a);
        System.out.println("Summation="  + summation);
    }

}

SHARE THIS
Previous Post
Next Post