Method overloading in java

Method overloading:-.It is the same method name with different signatures/arguments/parameters..It is not dependent on the return type. ie; there can be any return type for the method

-----------------------------------------------Program------------------------------------------------------

package java1;

import java.util.Scanner;

public class Java1 
{
    int a=1,b=1,c;
    
    int compute()
    {
        c=a+b;
        System.out.println("sum of the numbers "+c);
        return c;
    }
    int compute(int d)
    {
        c=d*b;
        System.out.println("multiplication of numbers: "+c);
        return c;
    }
    
    void compute(float d,int e)
    {
        float c;
        c=d+e;
        System.out.println("Sum of float and int: "+c);
        
    }
    
    void compute(String d,String e,char f)
    {
        String c=d+e+f;
        System.out.println(c);
    }
    public static void main(String[] args)
    {
        Java1 j=new Java1();
        Scanner sc=new Scanner(System.in);
        System.out.println("enter an integer:");
        int a=sc.nextInt();
        j.compute();
        j.compute(a);
        j.compute(10.5f,5);      //we need to specify the float number with--f--//otherwise the value is treated as double
        j.compute("hi","hello",'a');
        
    }
}

In the above programming example,

compute is the overloaded method having different arguments/parameters/signature  with independent return type.

Posted on by