|
JavaFAQ Home » Java Lessons by Jon Huhtala

The Java Lesson 15
Overloading class methods
Overview
It is possible in Java to define several methods which have
the same name but a recognizable difference in their parameters. This is
referred to as method overloading. When a call expression is encountered, the
compiler knows which of the identically named methods to call based upon the
parameters being passed.
Overloaded class methods
-
Can be defined within a single class or in
multiple classes having the same root class (to be covered in a later lesson).
-
Have the same method name.
-
Must differ in the number, data type, or order
of their parameters. This is necessary for the compiler to know which method
is which. The combination of the method name and its parameters is sometimes
known as the method's "signature" because it must be unique (with the
exception of method overriding which will be covered later).
For example, consider the following method
headers:
public static
double area(double length, double width)
public static
double area(double radius)
public static
double area(double top, double bottom, double height)
These represent the headers of methods that could be used to calculate the
area of a rectangle, the area of a circle, and the area of a trapezoid. If the
compiler encounters the call expression
area(2.3)
it knows to call the second area() method (to calculate the area of a circle) because that
method only accepts one double parameter. If the compiler encounters the call
expression
area(3.5,
1.7)
it knows to call the first area() method (to calculate the area of a rectangle) because
that method accepts two double parameters. And, if the compiler encounters the call
expression
area(4.2, 1.8,
3.4)
it knows to call the third area() method (to calculate the area of a trapezoid) because
that method accepts three double parameters.
- Can be impacted by the automatic widening of parameters. For example,
based upon the above method headers, the call expression
area(2)
would result in the compiler calling the second area() method (to calculate the area of a circle)
because the int being
passed can be widened to a double. If this is a problem, another overloaded method could be
defined which only accepts a single integer parameter. Its header might be
public static
double area(long radius)
If any type of integer is passed (byte, short, etc.), it would be widened to a long and passed to this overloaded method. Because
of automatic widening, care must be used in defining overloaded
methods.
- Can return different data types. As far as the compiler is concerned,
overloaded methods are different methods and may each return a different data
type. For example,
public static
void aMethod(byte parm1, float parm2)
public static
int aMethod()
public static
char aMethod(float parm1, byte parm2)
public static
boolean aMethod(double parm1)
are the headers of four different methods that happen to have
the same name. What you are NEVER allowed to do, however, is to have exactly
the same signatures but return different data types. For example, the
following method headers
public static
int aMethod(int x)
public static
char aMethod(int y)
would result in a compile error because the compiler wouldn't
know which method to call if it encountered a call expression such as
aMethod(17) Sample program
The following sample program calculates the amount due for an order.
Overloaded methods are used to perform the calculation differently depending
upon how many items are ordered and whether or not the item is taxable.
public class App {
public static void main(String[] args) {
//
Variables.
byte quantity;
double unitPrice; boolean
isTaxable; double amountDue; char
again;
// Constants.
final
double TAX_RATE = .06;
// Loop to process one
order.
do {
//
Get data from the user.
Utility.separator(50, '>');
System.out.print("Enter quantity ordered:
"); quantity =
Keyboard.readByte(); System.out.print("Enter
unit price: "); unitPrice =
Keyboard.readDouble();
System.out.print("Taxable item? (true or false):
"); isTaxable =
Keyboard.readBoolean();
// Process the
order data.
Utility.skip();
if (quantity <= 0 ||
unitPrice <= 0) {
System.out.println(" Invalid data");
} else if (quantity == 1 &&
isTaxable) {
System.out.println("Single item with tax: "
+
Utility.moneyFormat(amountDue(unitPrice,
TAX_RATE)));
} else if (quantity == 1 &&
!isTaxable) {
System.out.println("Single item without tax: "
+
Utility.moneyFormat(amountDue(quantity,
unitPrice)));
} else if (quantity > 1 &&
isTaxable) {
System.out.println("Multiple items with tax: "
+
Utility.moneyFormat(amountDue(quantity, unitPrice,
TAX_RATE)));
} else
{ System.out.println("Multiple
items without tax: "
+
Utility.moneyFormat(amountDue(quantity,
unitPrice)));
}
// Ask if they want to do another and
repeat as requested.
Utility.skip(); System.out.print("Again?
(Y/N): "); again =
Keyboard.readChar(); }
while (again == 'Y' || again ==
'y'); }
/** * Method to calculate the amount
due for an order of multiple * non-taxable items. This method
receives the quantity and the * unit price.
*/
public static double amountDue(int qty, double price)
{ return qty * price; }
/** * Method to calculate the amount due for an order of
multiple * taxable items. This method receives the quantity, the
unit * price, and the sales tax rate. */
public static double amountDue(int qty, double price, double rate)
{ return qty * price * (1 + rate);
}
/** * Method to calculate the amount due for an
order of a single * taxable item. This method receives the unit
price and the * sales tax rate. */
public
static double amountDue(double price, double rate) {
return price * (1 + rate); } }
Notes:
-
Although the overloaded methods are trivial
in this sample, they demonstrate how overloaded methods work. Be sure to
notice how their signatures differ and how they are called.
-
Don't be intimidated by the level of
cascading. Read cascaded code from the inside and work out. Focus on what
you get back from the inner method and see how that value can be used as a
parameter in calling the outer method. Being able to nest method calls
inside method calls is one of the strengths of Java.
Lab exercise for Ferris
students
E-mail your answers to this
assignment no later than
the due date listed in the class schedule.
Review questions
-
Based upon the following
class definition, which of the methods below can be legally defined at line
4? (choose three)
1 2 3 4 5 |
public
class MyMethods { public static int aMethod(int a, char b)
{ }
} |
-
public static int aMethod(int c, char d) {}
-
public static int aMethod(char b, int a) {}
-
public static byte aMethod(int x, char y) {}
-
public static byte aMethod(char b, int a) {}
-
public static void aMethod(char b, int a) {}
-
If a class named MyClass has methods with the headers
1 2 3 4 5 |
public
static void aMethod(float a, boolean b) public static void
aMethod(double a, boolean b) public static void aMethod(int a,
boolean b) public static void aMethod(long a, boolean b, char
c) public static void aMethod(int a, boolean b, char
c) |
Which of the methods would
be called by the following expression?
aMethod(123L, true)
-
method 1
-
method 2
-
method 3
-
method 4
-
method 5
-
What will happen when an
attempt is made to compile and execute the following program? The line numbers
are for reference purposes only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public
class App { public static void main(String[] args)
{ System.out.println("Result: " + aMethod(3,
2.0)); } public static int aMethod(int a, int b)
{ return a * b; } public
static double aMethod(int a, double b) { return
a + b; } public static double aMethod(double a,
int b) { return a - b;
} } |
-
a compile error will occur
at line 3
-
the program will compile
but an error will occur at run time
-
the program will compile
and run to display "Result: 6"
-
the program will compile
and run to display "Result: 5.0"
-
the program will compile
and run to display "Result: 1.0"
-
What will happen when an
attempt is made to compile and execute the following program? The line numbers
are for reference purposes only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public
class App { public static void main(String[] args)
{ short x = 7;
aMethod(x); System.out.println("Variable of x is
now " + x); } public static int aMethod(int a)
{ System.out.println("Received an
int"); return a + 1; }
public static double aMethod(double b) {
System.out.println("Received a double"); return
b + 1; } } |
-
a compile error will occur
at line 4
-
the program will compile
but an error will occur at run time
-
the program will compile
and run to display:
Received an int Value of x is now 7
-
the program will compile
and run to display:
Received an int Value of x is now 8
-
the program will compile
and run to display:
Received a double Value of x is now 7.0
-
the program will compile
and run to display:
Received a double Value of x is now 8.0
Printer Friendly Page
Send to a Friend
..
Search here again if you need more info!
|