Implementation of Java method overloading (changing the data types)
Codeaft.java
class Area
{
    static int rectangle(int a,int b)
    {
        return a*b;
    }
    static double rectangle(double a,double b)
    {
        return a*b;
    }
}

class Codeaft
{
    public static void main(String args[])
    {
        System.out.println("Area of Rectangle "+Area.rectangle(3.14,10.0)); 
        System.out.println("Area of Rectangle "+Area.rectangle(5,10)); 
    }
}
Output
codeaft@codeaft:~$ javac Codeaft.java
codeaft@codeaft:~$ java Codeaft Area of Rectangle 31.400000000000002 Area of Rectangle 50 codeaft@codeaft:~$
Implementation of Java method overloading (changing the number of arguments)
Codeaft.java
class Area
{
    static int rectangle(int a,int b)
    {
        return a*b;
    }
    static int rectangle(int a)
    {
        return a*a;
    }
}

class Codeaft
{
    public static void main(String args[])
    {
        //Every square is rectangle
        System.out.println("Area of Square "+Area.rectangle(5)); 
        System.out.println("Area of Rectangle "+Area.rectangle(5,10)); 
    }
}
Output
codeaft@codeaft:~$ javac Codeaft.java
codeaft@codeaft:~$ java Codeaft Area of Square 25 Area of Rectangle 50 codeaft@codeaft:~$
Comments and Reactions