Overloading in Java

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

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to demonstrate working of method

// overloading in Java.

publicclassSum {

// Overloaded sum(). This sum takes two int parameters

publicintsum(intx, inty)

{

return(x + y);

}

// Overloaded sum(). This sum takes three int parameters

publicintsum(intx, inty, intz)

{

return(x + y + z);

}

// Overloaded sum(). This sum takes two double parameters

publicdoublesum(doublex, doubley)

{

return(x + y);

}

// Driver code

publicstaticvoidmain(String args[])

{

Sum s = newSum();

System.out.println(s.sum(10, 20));

System.out.println(s.sum(10, 20, 30));

System.out.println(s.sum(10.5, 20.5));

}

}

chevron_right

filter_none

Output :

30
60
31.0

Question Arises:
Q. What if the exact prototype does not match with arguments.
Ans.
Priority wise, compiler take these steps:

Type Conversion but to higher type(in terms of range) in same family.

Type conversion to next higher family(suppose if there is no long data type available for an int data type, then it will search for float data type).

Let’s take an example to clear the concept:-

filter_none

editclose

play_arrow

linkbrightness_4code

classDemo {

publicvoidshow(intx)

{

System.out.println("In int"+ x);

}

publicvoidshow(String s)

{

System.out.println("In String"+ s);

}

publicvoidshow(byteb)

{

System.out.println("In byte"+ b);

}

}

classUseDemo {

publicstaticvoidmain(String[] args)

{

bytea = 25;

Demo obj = newDemo();

obj.show(a); // it will go to

// byte argument

obj.show("hello"); // String

obj.show(250); // Int

obj.show('A'); // Since char is

// not available, so the datatype

// higher than char in terms of

// range is int.

obj.show("A"); // String

obj.show(7.5); // since float datatype

// is not available and so it's higher

// datatype, so at this step their

// will be an error.

chevron_right

filter_none

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

filter_none

editclose

play_arrow

linkbrightness_4code

publicclassMain {

publicintfoo() { return10; }

// compiler error: foo() is already defined

publiccharfoo() { return'a'; }

publicstaticvoidmain(String args[])

{

}

}

chevron_right

filter_none

However, Overloading methods on return type are possible in cases where the data type of the function being called is explicitly specified. Look at the examples below :

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to demonstrate the working of method

// overloading in static methods

publicclassMain {

publicstaticintfoo(inta) { return10; }

publicstaticcharfoo(inta, intb) { return'a'; }

publicstaticvoidmain(String args[])

{

System.out.println(foo(1));

System.out.println(foo(1, 2));

}

}

chevron_right

filter_none

Output:

10
a

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to demonstrate working of method

// overloading in methods

classA {

publicintfoo(inta) { return10; }

publiccharfoo(inta, intb) { return'a'; }

}

publicclassMain {

publicstaticvoidmain(String args[])

{

A a = newA();

System.out.println(a.foo(1));

System.out.println(a.foo(1, 2));

}

}

chevron_right

filter_none

Output:

10
a

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 canoverload main() in Java. Refer overloading main() in Java for more details.

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.