The concept of Method Overloading in Java is where a class can have multiple methods with the same name, provided that their argument constructions are different. Method Overloading is comparable to constructor overloading where we can implement multiple constructors (also the same name), provided that these constructors have different argument construction.
Method Overloading in Java
- Method name should be exactly same. Java is case sensitive, so two methods with name
foo()
andfOO()
are totally different and doesn’t come under method overloading in java. - The difference between overloaded methods are the arguments. Overloaded methods should have different method arguments. For example, different number of parameters, different data types of parameters etc.
- Method return type have no significance for overloading, java compiler will complain about duplicate method in the class.
- Method parameter names have no significance in method overloading. So in terms of java,
foo(int index)
andfoo(int i)
are same. - Method overloading is also called compile time polymorphism. Read more at OOPS Concepts in Java.
Let’s look into some examples of method overloading in java.
Method Overloading in Java Example
1 2 3 4 5 6 7 8 9 |
package com.journaldev.methodoverloading; public class GetAverage { private static void gatherInput(int dataOne, int dataTwo) { }; private static void gatherInput(int dataThree, String dataFour, float dataFive) { }; } |
The snippet above shows a classic example of Method Overloading. Two gatherInput()
methods are declared under GetAverage
Class. In usual cases, java compiler would display the error: Duplicate method gatherInput() in type GetAverage
. However, in this case, we see that the two methods possess different argument list, which makes this approach in java permissible.
Instances where Method Overloading is permissible
- Parameter Quantity: Method Overloading is allowed within the class given that the number of parameters of the overloaded methods are not the same.
123456789package com.journaldev.methodoverloading;public class GetAverage {private static void gatherInput(int dataOne, int dataTwo) {};private static void gatherInput(int dataOne, int dataTwo, int dataThree) {};} - Parameter Data Types: Method Overloading is allowed within the class given that at least one pair of parameters, from each overloaded method, are of different data type.
12345678public class GetAverage {private static void gatherInput(int dataOne, int dataTwo) {};private static void gatherInput(int dataOne, double dataTwo) {};} - Order of Parameters: Method Overloading is allowed within the class given that the order of the data type variables are not the same for overloaded methods.
12345678public class GetAverage {private static void gatherInput(int dataOne, double dataTwo) {};private static void gatherInput(double dataOne, int dataTwo) {};}
Instance where Method Overloading is Impermissible
- Method Return Type: Method Overloading is prohibited within the class with methods having identical parameter list but with different method return type. Bear in mind that Overloading focuses on the parameters, not the return type.
12345678910public class GetAverage {private static int gatherInput(int dataOne, double dataTwo) {return 0;};private static double gatherInput(int dataOne, double dataTwo) {return 0;};}
Above code will produce compiler error:Duplicate method gatherInput(int, double) in type GetAverage
.
Type Promotion in Java
Type Promotion is a java concept where a data type of smaller rank is promoted to the data type of higher rank.
The figure below illustrates the data type size promotion order:
As can be observed from the above diagram, byte may be promoted to short. Int data type have three possible promotions: double, long, or float.
Type Promotion and Method Overloading in Java
Type Promotion goes hand in hand with Method Overloading in Java. Type Promotion extends the logic of method overloading when it comes to deciding which overloaded method is used, in cases where values passed does not coincide with any of the parameter data types established.
Note: Passed value data type is automatically promoted to its data type next in line (refer to figure above), if no matching data type is found.
Let’s look into some of the examples of type promotion and overloading in java.
-
- Type Promotion Example 1:
123456789101112131415package com.journaldev.methodoverloading;public class TypePromotionExample {void add(float dataOne, float dataTwo) {System.out.println(dataOne + dataTwo + " type float");}void add(long dataOne, long dataTwo) {System.out.println(dataOne + dataTwo + " type long");}public static void main(String[] args) {TypePromotionExample tpe = new TypePromotionExample();tpe.add(5, 5); // integer values are passed}}
Output: 10 type longTwo integers are passed in overloaded add() method. Since no overload methods possess (int, int) arguments, the passed values will be promoted to the nearest bigger type i.e. long.
- Type Promotion Example 2:
123456789101112131415package com.journaldev.methodoverloading;public class TypePromotionExample {void add(float dataOne, float dataTwo){System.out.println(dataOne+dataTwo + " type float");}void add(double dataOne, double dataTwo){System.out.println(dataOne+dataTwo + " type long");}public static void main(String[] args) {TypePromotionExample tpe = new TypePromotionExample();tpe.add(5, 5); // integer values are passed}}
Output: 10.0 type float
Here the nearest bigger type for int is float, hence the output.
- Type Promotion Example 3:
123456789101112131415package com.journaldev.methodoverloading;public class TypePromotionExample {void add(double dataOne, int dataTwo){System.out.println("1st method invoked.");}void add(long dataOne, long dataTwo){System.out.println("2nd method invoked.");}public static void main(String[] args) {TypePromotionExample tpe = new TypePromotionExample();tpe.add(5, 5); // integer values are passed}}
Here java compiler will produce error as
The method add(double, int) is ambiguous for the type TypePromotionExample
. It’s because both methods are applicable and type conversion is required for any one of them to execute, so java compiler can’t decide which one to go for.
- Type Promotion Example 1:
Method Overloading in Java Valid/Invalid Cases
Let’s look at some code snippets for method overloading use case, whether they are valid or not.
- Method Overloading in Java Example 1
1234void method(int dataOne, int dataTwo, int dataThree){}void method(int dataFour, int dataFive, int dataSix){}
Compile time error messageDuplicate method method(int, int, int)
. Even if the variable names differ, java only looks into data types being created and in this example, are both identical(int, int, int). - Method Overloading in Java Example 2
1234void method(int dataOne, int dataTwo){}void method(float dataThree, float dataFour){}
Valid case of method overloading. Data types of arguments are different. - Method Overloading in Java Example 3
1234void method(int dataOne, int dataTwo){}void method(int dataThree){}
Valid case of method overloading, number of arguments are different. - Method Overloading in Java Example 4
1234void method(int dataOne, float dataTwo){}void method(float dataThree, int dataFour){}
Valid case of method overloading. Sequence of the data types of parameters are different, first method having (int, float) and second having (float, int). - Method Overloading in Java Example 5
1234int method(int dataOne, float dataTwo){ return 0;}float method(int dataThree, float dataFour){ return 0.0;}
Compile time error because argument lists are identical. Even if return type of methods and variable names are different, it’s not a valid case. Since return type or variable name literals are not criteria for method overloading.
That’s all for method overloading in java.
Reference: Wikipedia