Overloading in Java

Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both. Overloading is related to compile time (or static) polymorphism.

// Java program to demonstrate working of method 
// overloading in Java. 

public class Sum {

    // Overloaded sum(). This sum takes two int parameters
    public int sum(int x, int y) {
        return (x + y);
    }

    // Overloaded sum(). This sum takes three int parameters
    public int sum(int x, int y, int z) { 
         return (x + y + z);
    }

    // Overloaded sum(). This sum takes two double parameters
    public double sum(double x, double y) { 
         return (x + y);
    }   

    // Driver code
    public static void main(String args[]) { 
        Sum s = new Sum();
        System.out.println(s.sum(10, 20));
        System.out.println(s.sum(10, 20, 30));
        System.out.println(s.sum(10.5, 20.5));
    }
}

Output :

30
60
31.0

What is the advantage?
We don’t have to create and remember different names for functions doing the same thing. For example, in our code, if overloading was not supported by Java, we would have to create method names like sum1, sum2,… or sum2Int, sum3Int, … etc.

Can we overload methods on return type?
We cannot overload by return type. This behavior is same in C++. Refer this for details

public class Main 
{
    public int foo() { return 10; }

    // compiler error: foo() is already defined
    public char foo() { return 'a'; }

    public static void main(String args[])
    { 
    }
}

Can we overload static methods?
The answer is ‘Yes’. We can have two ore more static methods with same name, but differences in input parameters. For example, consider the following Java program. Refer this for details.

Can we overload methods that differ only by static keyword?
We cannot overload two methods in Java if they differ only by static keyword (number of parameters and types of parameters is same). See following Java program for example. Refer this for details.

Can we overload main() in Java?
Like other static methods, we can overload main() in Java. Refer overloading main() in Java for more details.

// A Java program with overloaded main()
import java.io.*;
 
public class Test {
     
    // Normal main()
    public static void main(String[] args) {
        System.out.println("Hi Geek (from main)");
        Test.main("Geek");
    }
 
    // Overloaded main methods
    public static void main(String arg1) {
        System.out.println("Hi, " + arg1);
        Test.main("Dear Geek","My Geek");
    }
    public static void main(String arg1, String arg2) {
        System.out.println("Hi, " + arg1 + ", " + arg2);
    }
}

Output :

Hi Geek (from main)
Hi, Geek
Hi, Dear Geek, My Geek

Does Java support Operator Overloading?
Unlike C++, Java doesn’t allow user defined overloaded operators. Internally Java overloads operators, for example + is overloaded for concatenation.

What is the difference between Overloading and Overriding?

  • Overloading is about same function have different signatures. Overriding is about same function, same signature but different classes connected through inheritance.
    OverridingVsOverloading
  • Overloading is an example of compiler time polymorphism and overriding is an example of run time polymorphism.

Related Articles:

This article is contributed by Shubham Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.