- Forward


Overloading Methods
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • An Observation:
    • Sometimes we want to do the same thing but using slightly different information (e.g., increase a Weight by another Weight or by a number of pounds)
  • Method Overloading:
    • Two or more methods with the same name in the same class but with different signatures (e.g., in Java, different parameters)
Commonalities
Back SMYC Forward
  • Common Uses:
    • Handling Default Values
    • Different Similar Types
    • Two Parameters and Many "Parameters"
  • Common Kinds of Methods:
    • Constructors
    • Accessors and Mutators
    • static Methods in Utility Classes
  • Common Mistakes:
    • Include duplicate code in the overloaded methods
Overloading for Default Handling
Back SMYC Forward
  • Observation:
    • Sometimes we want a method to use default values and sometimes we want to supply all of the parameters
  • Solution:
    • Have two methods with the same name and different numbers of parameters
Overloading for Default Handling
Back SMYC Forward
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
Back SMYC Forward
  • Observation:
    • Sometimes we want to perform the same operation on different, but similar, types (e.g., int and double)
  • Solution:
    • Have two methods with the same name and different types of parameters
Overloading for Similar Types
Back SMYC Forward
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.)
Back SMYC Forward
  • Observation:
    • Calling a method with a similar type might be handled by automatic type conversion
  • Implementation:
    • Different languages handle this in different ways
Similar Types (cont.)
Back SMYC Forward
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"
Back SMYC Forward
  • Observation:
    • Sometimes we want to perform the same operation on two and "more than two" values
  • Solution:
    • Have two methods, one with two parameters and one with "more than two" parameters
Overloading for "Cardinality"
Back SMYC Forward
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.)
Back SMYC Forward

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

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; }
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
Back SMYC Forward
  • Common Use:
    • Mostly used for default handling
  • Terminology:
    • The default constructor has no parameters
    • The constructor that is passed an object of the same type is called a copy constructor
    • All others are called explicit value constructors
Overloaded Constructors (cont.)
Back SMYC Forward
  • Remember:
    • Constructors can call other methods
  • Automatic Constructor in Java:
    • A default constructor is created by Java if no constructor is provided
    • If any constructor is provided then you must explicitly include a default constructor if you need one
Avoiding Code Duplication
Back SMYC Forward
  • As We've Seen:
    • Overloaded methods should often call "other versions of themselves" to avoid code duplication
  • The Special Case of Constructors:
    • Constructors call "other versions of themselves" using this()
    • A call to another constructor must be the first statement in the constructor making the call
Overloaded Constructors (cont.)
Back SMYC Forward

An Example

javaexamples/oopbasics/pictureframe/overloadedconstructors/PictureFrame.java (Fragment: Constructors)
 
Overloaded Constructors (cont.)
Back SMYC Forward

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); }
There's Always More to Learn
Back -