Method Overloading in Java: Understanding Signatures and Implementation - Prof. Brian F. H, Study notes of Javascript programming

Method overloading in java, a feature that allows defining multiple methods with the same name but different signatures. The concept of method signatures, the importance of method overloading, and how java determines which method to call when an overloaded method is invoked. Examples are provided to illustrate the concepts.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-3jl-1
koofers-user-3jl-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 31
Programming Assignment
The next programming assignment is on the course web site. It's due in 2 weeks.
Method Overloading
Let's say that I have a method that returns the larger value of two doubles:
public double max( double a, double b ) {
if ( a > b )
return a;
else
return b;
}
Now, I can use this method with 2 doubles:
double first = stdin.nextDouble();
double second = stdin.nextDouble();
double larger = max( first, second );
I can also do this:
int first = stdin.nextInt();
int second = stdin.nextInt();
double larger = max( first, second );
This is OK, but not really that great. I could change the last statement to be:
int larger = (int) max( first, second );
This is still not so great - does a lot of unecessary work converting ints to doubles and
back again. I'd really like this:
int first = stdin.nextInt();
int second = stdin.nextInt();
int larger = max( first, second );
Java allows me to do this by defining a second method with the same name!
public int max( int a, int b ) {
if ( a > b )
return a;
else
return b;
pf3
pf4

Partial preview of the text

Download Method Overloading in Java: Understanding Signatures and Implementation - Prof. Brian F. H and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 31

Programming Assignment The next programming assignment is on the course web site. It's due in 2 weeks. Method Overloading Let's say that I have a method that returns the larger value of two doubles: public double max( double a, double b ) { if ( a > b ) return a; else return b; } Now, I can use this method with 2 doubles: double first = stdin.nextDouble(); double second = stdin.nextDouble(); double larger = max( first, second ); I can also do this: int first = stdin.nextInt(); int second = stdin.nextInt(); double larger = max( first, second ); This is OK, but not really that great. I could change the last statement to be: int larger = (int) max( first, second ); This is still not so great - does a lot of unecessary work converting ints to doubles and back again. I'd really like this: int first = stdin.nextInt(); int second = stdin.nextInt(); int larger = max( first, second ); Java allows me to do this by defining a second method with the same name! public int max( int a, int b ) { if ( a > b ) return a; else return b;

This is called method overloading. Method overloading is useful when we want to have methods that do similar tasks, but need to operate on

  • different types of parameters
  • different numbers of parameters Java allows a method name to be overloaded as long as the signature of the methods differs. A method's signature is
  • the method's name
  • the types of the parameters
  • the order of the parameters
  • the number of parameters Note that the signature does NOT include
  • the parameter names
  • the return type The book is WRONG - in section 7.6 on page 344 it says that the method signature includes the names of the parameters - this is NOT TRUE. Let's look at some examples - what is the signature of public boolean isIsoceles(); public static max( int a, int b ); public static double sqrt( double x ); public static int sqrt( int x ); public We've actually seen method overloading already without realizing it: System.out.println() String.valueOf() Without overloading, we would have to have separate methods for each of these - printString, printInt, printDouble, printChar, etc. How does Java decide which method to use when an overloaded method is called? First, it sees if there is an exact type match of actual and formal parameters. Then, it widens actual parameters as necessary to match. public static void main( String[] args ) { System.out.println( "the larger of 3 and 5 is " + max( 3, 5 ) ); System.out.println( "the larger of 3.2 and 4.7 is " + max( 3.2, 4.7) ); System.out.println( "the larger of 3 and 4.7 is " +

Lab Exercise Modify the Currency class by adding the following overloaded methods:

  1. public boolean isEqual( Currency c ) – this should return true if amount and the currency name are the same in both Currency instances.
  2. public boolean isEqual( double amount, String currencyName ) – this should return true if the amount and currencyName in the Currency instance are the same values passed in the formal parameters. Write a CurrencyTest class to test these new methods. The Currency class is available on O: in the source code/Chapter 7 directory.