JMU
Overloading Methods
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Motivation
Commonalities
Overloading for Default Handling
Overloading for Default Handling
    public class TaxStand
    {
          .
          .
          .
          /** 
           * Calculates the tax payment based on income and
           * filing status
           */
          public double payment(int income, int status)
          {
             ...
          }

          /**
           * Calculates the tax payment assuming the
           * filing status is 0
           */
          public double payment(int income)
          {
              return payment(income, 0);
          }
    }
    
Overloading for Similar Types
Overloading for Similar Types
    public class Weight
    {
        private int ounces, pounds;
        .
        .

        /**
         * Increase this Weight by a given amount
         */
        public void increaseBy(Weight amount)
        {
            increaseBy(amount.pounds, amount.ounces);
        }


        /**
         * Increase this Weight by a given amount
         */
        public void increaseBy(int pounds, int ounces)
        {
           ...
        }
    }
    
Similar Types (cont.)
Similar Types (cont.)
    public class TaxStand
    {
          .
          .
          .
          /** 
           * Calculates the tax payment based on income and
           * filing status
           */
          public double payment(int income, int status)
          {
              // In Java, this method isn't needed because
              // the int will be widened
          }


          /** 
           * Calculates the tax payment based on income and
           * filing status
           */
          public double payment(double income, int status)
          {
             ...
          }

    }
    
Overloading for "Cardinality"
Overloading for "Cardinality"
    public int minimum(int a, int b)
    {
        int min;

        min = a;
        if (b < a) min = b;

        return min;
    }

    public int minimum(int a, int b, int c)
    {
        int min, temp;

        temp = minimum(a, b);
        min  = minimum(temp, c);

        return min;
    }
    
Overloading for "Cardinality" (cont.)

Using an Array to Handle an Arbitrary Number of Values

    public int minimum(int data[])
    {
        int min;

        min = data[0];
        for (int i=1; i<data.length; i++) {
            if (data[i] < min) min = data[i];
        }
        return min;
    }
    

A More Flexible Approach: Using a Variable Length Parameter

      The advantage of the flexible approach is that this method
      can be passed an array or any number of int values
      (e.g., minimum(5, 3, 1, 7)).
    

Overloaded Constructors
Overloaded Constructors (cont.)
Avoiding Code Duplication
Overloaded Constructors (cont.)

An Example

javaexamples/oopbasics/pictureframe/overloadedconstructors/PictureFrame.java (Fragment: Constructors)
    /**
     * Construct a PictureFrame object.
     *
     * Note: Negative values are converted to positive values 
     * and the width and height are put in canonical form 
     * (i.e., a portrait orientation).
     *
     * @param width   The width (in inches)
     * @param height  The height (in inches)
     * @param matte   The size of the matte (in inches) on all 4 sides
     * @param stand   true if there is a built-in stand
     */
    public PictureFrame(double width, double height, double matte, 
                        boolean stand)
    {
        double     h, w;
        
        h = Math.abs(height);
        w = Math.abs(width);

        this.width  = Math.min(w, h);
        this.height = Math.max(w, h);
        this.matte  = Math.abs(matte);
        this.stand  = stand;
    }
    
    /**
     * Construct a PictureFrame object with no matte and no stand.
     *
     * @param width   The width (in inches)
     * @param height  The height (in inches)
     */
    public PictureFrame(double width, double height)
    {
        this(width, height, 0.0, false);
    }
    
    /**
     * Default Constructor.
     *
     * Construct a 3x5 PictureFrame with no matte and no stand.
     */
    public PictureFrame()
    {
        this(3.0, 5.0, 0.0, false);
    }
    
    /**
     * Copy Constructor.
     *
     * @param other   The PictureFrame to copy
     */
    public PictureFrame(PictureFrame other)
    {
        this(other.width, other.height, other.matte, other.stand);
    }
        
Overloaded Constructors (cont.)

A Common Mistake

    public PictureFrame(double width, double height, double matte, 
                        boolean stand)
    {
        double     h, w;
        
        h = Math.abs(height);
        w = Math.abs(width);

        this.width  = Math.min(w, h);
        this.height = Math.max(w, h);
        this.matte  = Math.abs(matte);
        this.stand  = stand;
    }
    
    public PictureFrame(double width, double height)
    {
        PictureFrame     frame;

        frame = new PictureFrame(width, height, 0.0, false);
    }