Overloading Methods

Overloading Methods
In some circumstances, you might want to write several methods in the same class that do the same basic job with different arguments. Consider a simple method that is intended to output a textual representation of its argument. This method could be called println().
Now suppose that you need a different print method for printing each of the int, float, and String types. This is reasonable, because the various data types require different formatting and, probably, varied handling. You could create three methods, called printInt(), printFloat(), and printString(), respectively. However, this is tedious.

The Java programming language, along with several other programming languages, permits you to reuse a method name for more than one method. This works only if there is something in the circumstances under which the call is made that distinguishes the method that is needed. In the case of the three print methods, this distinction is based on the number and type of the arguments.

By reusing the method name, you end up with the following methods:

public void println(int i)

public void println(float f)

public void println(String s)

When you write code to call one of these methods, the appropriate method is chosen according to the type of argument or arguments you supply.

Two rules apply to overloaded methods:

● Argument lists must differ.

The argument lists of the calling statement must differ enough to allow unambiguous determination of the proper method to call. Normal widening promotions (for example, float to double) might be applied; this can cause confusion under some conditions.

● Return types can be different.

The return type of the methods can be different, but it is not sufficient for the return type to be the only difference. The argument lists of overloaded methods must differ.

Methods Using Variable Arguments

A variation on overloading is when you need a method that takes any number of arguments of the same type. For example, imagine you need to create a method that calculates the average of a set of integers. You could define the following methods:

public class Statistics {

public float average(int x1, int x2) {}

public float average(int x1, int x2, int x3) {}

public float average(int x1, int x2, int x3, int x4) {}


These methods can be invoked as follows:

float gradePointAverage = stats.average(4, 3, 4);

float averageAge = stats.average(24, 32, 27, 18);

These three overloaded methods all share the same functionality. It would be nice to collapse these methods into one method. J2SE version 5.0 now provides a feature, called varargs or variable arguments, to enable you to write a more generic method:

public class Statistics {

public float average(int… nums

int sum = 0;

for ( int x : nums ) {

sum += x;


return ((float) sum) / nums.length;



This new varargs method can be invoked in the same manner as the suite of overloaded methods. Notice that the nums argument is an actual array object of type int[]; this permits the method to iterate over the elements and to get the number of elements (that is, the length of the array).

Overloading Methods