Java Basics

Methods 6 - Overloading

Purpose of this lesson: Programming ideas

Here is a small program which simply computes the average of three numbers. It uses three overloaded methods to read the numbers. For such a small program you would not use three different methods, of course, but this shows how overloaded methods are defined and used. It's very common for one overloaded method to call another. another variation of the program, this time using three methods. Altho there is no real need for these methods in such a small program, large programs are in fact composed of many small methods. It is the essential way that all code is structured.

Each of the user-defined method names, both in the call and the definition, is hilited.

Good practices

Example of overloading - averaging three values

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
// File   : methods/avg3/AvgThreeOverloaded.java
// Description: Averages three numbers -- meaningless, but
// Purpose: Show an overloaded method, getDouble, with three definitions,
//          differing in the number of parameters.
// Issues : Input isn't checked for legality (non-null number) because
//          the point is to show overloading.
// Author : Fred Swartz - 2007-01-11 - placed in public domain

import javax.swing.*;

public class AvgThreeOverloaded {

    //============================================================== main
    public static void main(String[] args) {
        //... Read three numbers using the three different methods.
        //    Using three different methods is only to show overloading.
        double n1   = getDouble();
        double n2   = getDouble("Enter the second number.");
        double n3   = getDouble("Enter last number.", 0.0, 100.0);
        double average = (n1 + n2 + n3) / 3.0;
        displayString("Average is " + average);
    }

    //========================================================= getDouble
    // I/O convenience method to read a double value.
    //     This version of the getDouble method simply calls on another
    //     version passing it a generic input message.
    private static double getDouble() {
        return getDouble("Enter a number");
    }

    //========================================================= getDouble
    // I/O convenience method to read a double value given a prompt.
    //     This version of getDouble displays the user supplied prompt.
    private static double getDouble(String prompt) {
        String tempStr;
        tempStr = JOptionPane.showInputDialog(null, prompt);
        return Double.parseDouble(tempStr);
    }

    //========================================================= getDouble
    // I/O convenience method to read a double value in a range.
    //     It builds a new prompt and calls another version to get
    //     the value, looping until a value in the range is found.
    private static double getDouble(String prompt, double low, double high) {
        double result;
        String rangePrompt = prompt + "  Value must be in range " 
                                    + low + " to " + high;
        
        //... Read and loop back if the number is not in the right range.
        do {
            result = getDouble(rangePrompt);
        } while (result < low || result > high);
        
        return result;
    }

    //===================================================== displayString
    // I/O convenience method to display a string in dialog box.
    private static void displayString(String output) {
        JOptionPane.showMessageDialog(null, output);
    }
}

Don't confuse overloading and overriding

This two terms are easily confused because they both have to do with multiple definitions of methods. Better terms would have been nice, but these are what we have. Overloading is making multiple method definitions which differ in the number or types of parameters, as described here. Overriding is redefining a method in a super class, using exactly the same number and types of parameters.

Review Questions

[TODO]

Programming Problems