Method overloading falls into the category of static polymorphism which is also called compile-time polymorphism. Two methods are said to be overloaded methods if and only if both methods have the same name but different argument types. Method overloading reduces the complexity of programming.
In Java, method signature consists of method name followed by names argument types. The return type of method is not a part of method signature.
public void method (int, char) (Bold highlighted part represents method signature)
Within a class, we cannot declare two methods with the same signature.
Note – In C language, method overloading concept is not available.
Java supports method overloading which reduces the complexity of programming.When we do method overloading, the Compiler is responsible for method resolution based on reference type.
So, if we make a child class object using parent class reference and then call an overloaded method, java will call parent class’ method because the reference of parent class was used while making child class object.
Example of Method Overloading
class Display { public void print() { System.out.println("This no args method wants to print nothing"); } public void print(int i) { System.out.println("This int args method wants to print " + i); } public void print(float f) { System.out.println("This float args method wants to print " + f); } } public class Demo { public static void main(String[] args) { Display d = new Display(); d.print(); d.print(10); d.print(5.6f); d.print('a'); d.print(15L); } }
In the above code, we first called three overloaded methods by passing valid argument types.
And accordingly, JVM will execute the methods which match with these arguments.But it is interesting to see what happens when we pass a character or a long as an argument to print() method since we haven’t defined such print() method.
In overloading, while resolving overloaded methods, if an exact matched method is not available, then we will not get any compile time error immediately.
First, it will promote argument to the next level and check whether a matched method is available or not. If a matched method is available, then JVM executes that method.
But, if the matched method is not available for all possible levels of argument, then we will get a compile-time error.
The following are all possible promotions in overloading.
byte -> short -> char -> int -> long -> float -> double
This process is called automatic promotion in overloading.
Here, int argument method and float argument method are called for character argument and long argument respectively.
Look at the below output for better understanding.
Output:
This no args method wants to print nothing This int args method wants to print 10 This float args method wants to print 5.6 This int args method wants to print 97 This float args method wants to print 15.0
Note the fact that while resolving overloaded methods, the compiler will always give priority to child type argument when compared with parent type argument.
Let’s understand with an example.
class Bird { } class Sparrow extends Bird { } class Test { public void print(Bird b) { System.out.println("Bird version is printed"); } public void print(Sparrow s) { System.out.println("Sparrow version is printed"); } } public class Demo { public static void main(String[] args) { Test test = new Test(); Bird b = new Bird(); Sparrow s = new Sparrow(); Bird obj = new Sparrow(); test.print(b); test.print(s); test.print(obj); } }
In this code, we create an object for Bird class and an object for Sparrow class.
When we pass these objects as an argument to print method, JVM will execute that print method whose argument matches with the reference class that was taken while creating an object.
Output:
Bird version is printed Sparrow version is printed Bird version is printed
This is all about method overloading in Java. Hope you find this tutorial helpful.