Method Overloading in Java

A class can have more than one method with same name but different argument list. This phenomenon is called method overloading. For example following two methods are overloaded :

Method overloading is how Java attains compile time polymorphism. By looking at the number and type of arguments, Java compiler can decide which method to call at the compile time. This is called static binding or early binding.


1. Ways to Overload a Method

There are three ways a method can be overloaded but all depend on differing argument list. The argument list can differ in three ways:

  1. Number of arguments in argument list.
  2. Type of the arguments in argument list.
  3. Order of argument types in argument list.

1.1 Method Overloading : Different number of Arguments

You can overload a method such that different definitions of the method take different number of arguments.

Output :

This example has three methods(excluding main). They take 2, 3 and 4 arguments respectively. Compiler automatically selects the version of method to be called by looking at the number of arguments passed to the method.


1.2 Method Overloading : Different Type of Arguments

You can overload a method such that different definitions of the method take same number of arguments but different type of arguments.

Output :

This example has two methods(excluding main). First definition is called when arguments are integer and the second definition is called when the arguments are floating point numbers.


1.3 Method Overloading : Different Order of Argumnet Types

This is no different from the last, but still people make mistake. So here it is. It would be better to understand it from example :

Output :

This example has two methods(excluding main). They have same number of arguments, ony the order of data type of arguments differs.

  • First definition is called when first argument is integer and second argument is double.
  • Second definition is called when first argument is double and second argument is integer.

2. What Method Overloading is not

Each of the following cases would result in compilation error:

  • Two methods in a class cannot be carbon copy of each other.
  • Two methods differing only in return type aren’t overloaded methods.
  • Two methods(having same name) with different parameter names but same number and type of arguments are the same.

     

3. Method Overloading and Other Topics

When you call a method, Java decides the method to be called by looking at the number and type of arguments passed. If an exact match is found then that method is called else one of the following happens:

  • Type promotion : Java type promotes the arguments i.e it promotes the narrower types to wider types. Like int is promoted to long, long is promoted to float and float is promoted to double etc.
  • Auto-boxing, Auto-unboxing : If the type promotion cannot find the method to be called then auto-boxing and auto-unboxing kicks in:
  • Varargs : If auto-boxing and auto-unboxing cannot find the exact match then Varargs come in picture.

Remember only one one of the three happens. First type promotion then Boxing and only then Varargs happen. As soon as the match is found other steps are skipped.


3.1 Example : Type promotion

Type promotion happens as in the diagram shown below :

type promotion in Java
type promotion in Java

If needed byte type is promoted to short, short is promoted to int and so on.

Output :

 


3.2 Example : Auto-boxing, Auto-unboxing

If needed some primitive types are automatically converted to Object types. Like int is converted to Integer, double to Double, long to Long and so on.

Output :

 


3.3 Example : Varargs

Java first tries find a method with exact match. If that fails then it tries type promotion, auto-boxing / auto-unboxing and if they all fail then Varargs are tried.

Output :

 


3.4 A mixed Example

Let’s look at a mixed example where following three work simultaneously:

  • Widening
  • boxing
  • Varargs

Output :

Explanation :

  • In method(7) , 7 is an int. It had three options:
    • It could be promoted to long and long could be promoted to float or
    • It could be auto-boxed to Integer or
    • It could be read as Vararg by method(int… x)
  • Type promotion took preference.

  • In method(7.0) , 7.0 is double literal float literal has f attached to its end like 7.0f

  • In method(7L) , 7L is long data type. It was type promoted to float.

  • In method(Double.valueOf(7.0)) , Double.valueOf(7.0) returns the Double object.
    • Double could be type promoted to Object.
    • Double could be auto-unboxed to double.
  • type promotion took preference.

4. Ambiguity leads to error

In the example below:

The call to method m(1, 2) is ambiguous. Java compiler cannot decide which version of the method to call.

  • Should it type promote first argument(1) to float and call m(float, int) or
  • Should it type promote second argument(2) to float and call m(int, float).

Keep any eye for such method definitions.


4. Miscellaneous Topics

Let’s look at some more topics related to method overloading.

4.1 Overloading Main Method

Note that any method can be overloaded, even the main method. You can have multiple definitions for main method in your program. You can call them as you want but the JVM will call the one with array of String as parameter. It would be the entry point for your program because this is how java has been designed.

Output :

 


5.2 Overloading vs Overriding

These are two different concepts:

  • Overloading refers to a secnario where a class has different methods with same name.
  • Overriding is related to inheritance where method in derived class hides the method in base class.