Overloading

When constructing a new object, it would be useful to have a certain amount of flexibility. For example, sometimes we have enough information to create a new Length object with specific data for the value and scale field, and at other times we might need to create an invalid Length object first and change it to a valid one when the necessary information becomes available. That idea of multiple use of one method - in this case the constructor - is supported using the object-oriented concept of overloading:

Overloading

Overloading is the ability to use one method to perform different tasks. Methods to be overloaded must have the same method name, but differ either in the types of input parameters, or in the number of input parameters, or both.

Example:

Suppose we have a class that has methods to double its input data. Rather then defining methods like doubleInt and doubleString, use overloading to accomplish the same feat. The data to be doubled should be the input value. You only need to provide the method headers.

Without overloading, the methods might have looked as follows:

public void doubleInt(int data)
{ // implementation }
public void doubleString(String data)
{ // implementation }

Using overloading, we define two methods that are both called doubleIt but have different input parameters:

public void doubleIt(int data)
{ // implementation }
public void doubleIt(String data)
{ // implementation }

This does not make the code shorter, but for the user of the class it is now a lot simpler. All they have to remember is that the method doubleIt doubles input data; they do not need to remember two different method names.

Example:

Suppose we are working on a class to deal with geometric points in two and three dimensions. Write a complete method, or methods, that compute and return the distance to the origin for these points.

Recall that the distance to the origin is given by the formulas:

 if (x,y) is a point in 2-dimensional space, its distance to the origin is 
if (x,y,z) is a point in 3-dimensional space, its distance to the origin is 

 Rather than using two different methods, we use overloading:

public double distance(double x, double y)
{
   return Math.sqrt(x*x + y*y); 
} 
public double distance(double x, double y, double z) 
{ 
   return Math.sqrt(x*x + y*y + z*z); 
}

Again, the user only needs to remember that the method distance will compute the distance to the origin, regardless of the dimensions of the input points.

Overloading Constructors

One of the most common uses of this mechanism is to overload the constructor of a class. In our Length class, it would make sense to use, for example, two different constructors: one to allocate memory for a Length object whose data fields are as of now undetermined, and a second one in case the data fields should be filled with values:

Example:

Provide two constructors to our Length class. The first one is as before while the second one initializes the data fields of the class.

 Recall that the constructor’s name is always the same as the class name. Therefore, our constructors are:

public Length()
{
   value = 0;
   scale = 'n'; 
} 
public Length(double _value, char_scale) 
{ 
   value = _value; 
   scale = _scale;
}

Example:

Recall that in our Rectangle class, we used a constructor with no arguments. Add a second constructor that takes the width and height of the rectangle as argument. Also, modify the 'ShapeTest' class accordingly.

Here is the code for both constructors.

Rectangle()
{
   name = "Rectangle";
   width = height = 0;
}
public Rectangle(double _width, double _height)
{
   name = "Rectangle";
   width = _width;
   height = _height;
}

Now we can change our ShapeTest class as follows: Instead of saying

Rectangle r = new Rectangle();
r.setDimensions(2.0, 3.0);

we can now simply say:

Rectangle r = new Rectangle(2.0, 3.0);

At the same time, our previous constructor will still work, so all programs that previously used to utilize our Rectangle class will still work without change. But, we can also change to those programs appropriately to make use of our new possibility.

Note:

You can overload any method (with some minor exceptions), and you can overload it as often as you need to. All that needs to be different is the type or number of input parameters to the various methods.

Overloading does not save you, the programmer, any work. You still have to write the same amount of code. But, it does save the user of your class a lot of trouble - they need to memorize only a few methods, and they can be ensured that via overloading the methods will do whatever is appropriate for the particular type of input parameter, automatically.

(bgw)