Overloading

Overloading

<-- Previous

Table of Contents

Next -->

When implementing a Java class, it sometimes becomes necessary to write more than one constructor for the class. Take our BankAccount class for example. This class has two constructors:

//default bank account with zero initial balance
BankAccount()
//bank account with initial balance
BankAccount(double initialBalance)

Java decides which constructor to use based on the parameters supplied by the user. When we write more than one constructor for the same class, we say that we are overloading the constructor.

Other methods can be overloaded as well.

Class Die

Consider a standard die with six faces and spots on each face - the spots are arranged on the die so that any two opposite faces add up to 7. The die can be thrown to expose the number of spots on the uppermost face. This is a way to randomly choose any number between 1 and 6, because each number on the die has an equal chance of being thrown, provided the die is fair.

We can model a die with a Java class, because Java has the Math.random() method, which generates a random number between 0 and 1, exclusive. To produce a random integer between 1 and a second higher integer, we must multiply the value generated by Math.random() by the higher integer, add 1, and then cast the result to an integer because Math.random() generates a double:

//random number between 1 & 6
int result = (int)(Math.random() * 6 + 1);

Returning to class Die:

Note the use of System.out.print(myDie.throwMe() + " "); - this method always prints its argument on the current line, so each throw of the Die is printed onto the same line, separated by a space.

This is fine as far as it goes, but a die can have more or less than 6 faces - in some role playing games 10- or 12-sided dice are used. If we want to build a Java class that represents a die, we have to take all the possibilities into account. To make our class more complete, we should allow for all possibilities. Also, sometimes we might want to throw the Die more than once, and we might want to know what the total is of all the throws. Consider the revised Die class below:

 

The Die class now has an overloaded constructor and an overloaded throwMe() method. Overloaded methods carry the same name, but have different parameters and possibly different return types (a constructor is a special kind of method). In this case we have:

//default Die
public Die(){
   sides = 6;
   pipCount = 0;
}

//n-sided Die
public Die(int numSides){
   sides = numSides;
   pipCount = 0;
}

When you place a call to the class constructor, Java sees two constructors of the same name. It then checks to see what the parameters of the two constructors are, and will base its decision on which constructor to use by the arguments and their data types present in the call to the constructor. In the TestDie class above,
Die myDie = new Die(12);
calls the second of the two constructors because it carries an integer argument - the first constructor has no parameters, so Java cannot use that one.

In the case of the throwMe() method, the situation is essentially similar - we have one method that takes no arguments, and one which takes an integer argument; Java will select which method to use based on the arguments you supply in the method call. Note that the two throwMe() methods apart from having different parameters, in this case have different return types: the first returns an integer, the second a string.

public int throwMe() { }

public String throwMe(int times) { }

Factors which affect the choice of overloaded methods are:

  1. The number of parameters
  2. The data type(s) of parameters

Note that merely changing the return data type is not sufficient to overload a method, and the Java compiler will not allow overloaded methods with the same parameters and merely different return data types.

Documentation for class Die.

<-- Previous

Table of Contents

Next -->

© G. Hearn, & University of the Western Cape, 2006

Citation: Overloading. (2008, July 16). Retrieved May 19, 2013, from UWC Free Courseware Web site: http://freecourseware.uwc.ac.za/freecourseware/information-systems/java-platform-introduction/overloading.
Copyright 2007-2008, by the Contributing Authors. This work is licensed under a Creative Commons License : Attribution-ShareAlike 3.0. Creative Commons License : Attribution-ShareAlike 3.0