exampracticeonline.com
Overloading

Method Overloading in Java

Method overloading is one of the ways through which java supports polymorphism. Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different.

Overloading always occur in the same class(unlike method overriding).

Method overloading can be done by changing number of arguments or by changing the data type of arguments. If two or more method have same name and same parameter list but differs in return type are not said to be overloaded method

NOTE: 1. Overloading always occur in the same class(unlike method overriding).

           2. Overloaded method can have different access modifiers.

Three ways to overload a method

  1. Different number of parameters.
  2. Different data type of parameters.
  3. Different sequence of data type of parameters.

1. Different number of parameters.

If two or more methods having same name but have different number of parameters.

Example: In below example we have two sum methods but first sum method accept two argument and second sum method accept three parameters. So, this is method overloading.

package com.jepo;

class OverloadingExample
{
    public void sum(int a, int b)
    {
        int sum = a+b;
        System.out.println("Sum of two : "+sum);
    }
    public void sum(int a, int b, int c)
    {
        int sum = a+b+c;
        System.out.println("Sum of three : "+sum);
    }
    public static void main(String args[])
    {
        OverloadingExample obj = new OverloadingExample();
        obj.sum(2,5);
        obj.sum(2,5,3);
    }
}


Output:
Sum of two : 7
Sum of three : 10

2. Different data type of parameters.

If two or more methods having same name but have different data type of parameters.

Example: In below example we have two show methods but first show method accept int parameter and second show method accept String parameter. So, this is method overloading.

package com.jepo;

class OverloadingExample
{
    public void show(int a)
    {
        System.out.println("Value of a : "+a);
    }
    public void show(String str)
    {
        System.out.println("Value of str : "+str);
    }
    public static void main(String args[])
    {
        OverloadingExample obj = new OverloadingExample();
        obj.sum(5);
        obj.sum("Hello Java");
    }
}


Output:

Value of a : 5

Value of str : Hello Java

3. Different sequence of data type of parameters.

If two or more methods having same name but have different sequence of data type of parameters.

Example: In below example we have two show methods but first sum method accept two argument(int and String) and second show method accept two argument(String, int). So, this is method overloading.

package com.jepo;

class OverloadingExample
{
    public void show(int a, String str)
    {
        System.out.println("Value of a : "+a+" and str : "+str);
    }
    public void show(String str, int a)
    {
        System.out.println("Value of str : "+str+" and a : "+a);
    }
    public static void main(String args[])
    {
        OverloadingExample obj = new OverloadingExample();
        obj.sum(5, "Hello Java");
        obj.sum("Hello Java", 5);
    }
}


Output:

Value of a : 5 and str : Hello Java

Value of str : Hello Java and a : 5

What is the advantage of Overloading?

We don’t have to create different names for functions having same functionality. So it reduce the complaxity of the code.

Example: If we want to create sum method for two parameters and for three paramenters. So we can define same name two methods of "Sum" that accept two and three parameters.

Can we overload static methods?

Yes’. we can have two ore more static methods with same name in our Class, but we have to follow the mehtod overloading principle for their input parameters.

Example:

package com.jepo;

class OverloadingExample
{
    public static void sum(int a, int b)
    {
        int sum = a+b;
        System.out.println("Sum of two : "+sum);
    }
    public static void sum(int a, int b, int c)
    {
        int sum = a+b+c;
        System.out.println("Sum of three : "+sum);
    }
    public static void main(String args[])
    {
        sum(2,5); // static method can call without object
        sum(2,5,3); // static method can call without object
    }
}


Output:
Sum of two : 7
Sum of three : 10

Can we overload methods on return type?

"No". We cannot overload by return type. If we try to overload methods on return type it will throw compile time exception.

Example:

package com.jepo;

class OverloadingExample
{
    public int show(int a, String str)
    {
        System.out.println("Value of a : "+a+" and str : "+str);

        return a;
    }
    public String show( int a, String str)  //compile time exception: duplicate show()
    {
        System.out.println("Value of str : "+str+" and a : "+a);

        return str;
    }
    public static void main(String args[])
    {
        OverloadingExample obj = new OverloadingExample();
        obj.sum(5, "Hello Java");
        obj.sum("Hello Java", 5);
    }
}


Output : Duplicate method sum(int, String) in type OverloadingExample (Compile time)

Can we overload methods that differ only by static keyword?

"No". We cannot overload two methods in Java if they different only by static keyword.

Can we overload main() in Java?

"Yes". we can overload main method and follow all the principle of static method overloading.

Example:

package com.jepo;

class OverloadingExample
{

    // JVM call only this main by default
    public static void main(String args[])
    {
        System.out.println("OverloadingMain.main() : first");
    }

    public static void main(String args[], String args[])
    {
        System.out.println("OverloadingMain.main() : Second");
    }
}


Output: OverloadingMain.main() : first

Method Overloading and Type Promotion

When a data type of smaller size is promoted to the data type of bigger size than this is called type promotion, for example: byte data type can be promoted to short and a short data type can be promoted to int, long, double etc.

Example:

package com.jepo;

class OverloadingExample
{
    public void sum(long a, long b)
    {
        int sum = a+b;
        System.out.println("Sum of two : "+sum);
    }
    public void sum(int a, int b, int c)
    {
        int sum = a+b+c;
        System.out.println("Sum of three : "+sum);
    }
    public static void main(String args[])
    {

        OverloadingExample obj = new OverloadingExample();
        obj.sum(2,5); // int type promoted to long
        obj.sum(2,5,3);
    }
}


Output:

Sum of two : 7
Sum of three : 10

Matching type promotion in Method Overloading

If there are matching type arguments in the method, type promotion is not performed.

Example :

package com.jepo;

class OverloadingExample
{
    public void sum(long a, long b)
    {
        long sum = a+b;
        System.out.println("Sum of two with long data type : "+sum);
    }
    public void sum(int a, int b)
    {
        int sum = a+b;
        System.out.println("Sum of two with int data type  : "+sum);
    }
    public static void main(String args[])
    {

        OverloadingExample obj = new OverloadingExample();
        obj.sum(2,5); // best match will call
    }
}


Output: Sum of two with int data type  :7

Ambiguity matching type promotion in Method Overloading

If two or more method promotes similar number of arguments, there will be ambiguity.

Example:

package com.jepo;

class OverloadingExample
{
    public void sum(int a, long b)
    {
        long sum = a+b;
        System.out.println("Sum of two with long data type : "+sum);
    }
    public void sum(long a, int b)
    {
        long sum = a+b;
        System.out.println("Sum of two with int data type  : "+sum);
    }
    public static void main(String args[])
    {

        OverloadingExample obj = new OverloadingExample();
        obj.sum(2,5); // compile time error
    }
}


Output : The method sum(int, long) is ambiguous for the type OverloadingExample