Overloading Overloading Basics o We have seen that
Overloading
Overloading Basics o We have seen that you can give the same name to methods in different classes. o You can also give the same name to methods within the same class. o This is called overloading. o In order for this to work there must be something different about the parameter lists to the two methods.
Overloading Basics (cont’d) o For example, one method may have three parameters, another with the same name may have only two parameters, and yet another may have parameters of a different type. o When Java encounters the invocation of a method name which may have different definitions, it selects the one for which the arguments in the invocation match the number and types of the parameters in the method definition.
Overloading Basics (cont’d) o If Java cannot find a method which exactly matches the types of the arguments, it will try to make some simple type conversions. o The Math class uses overloading for some of its methods. o That is, it decides which version of a method to use based on the types of the arguments.
Overloading Example o /** This is just a toy class to illustrate overloading. */ public class Statistician { public static void main(String[] args) { double average 1 = Statistician. average(40. 0, 50. 0); double average 2 = Statistician. average(1. 0, 2. 0, 3. 0); char average 3 = Statistician. average('a', 'c'); System. out. println("average 1 = " + average 1); System. out. println("average 2 = " + average 2); System. out. println("average 3 = " + average 3); } public static double average(double first, double second) { return ((first + second)/2. 0); } public static double average(double first, double second, double third) { return ((first + second + third)/3. 0); } } public static char average(char first, char second) { return (char)(((int)first + (int)second)/2); }
Another Overloading Example Pet - name: String - age: int - weight: double + write. Output(): void + set(String new. Name): void + set(int new. Age): void + set(double new. Weight): void + set(String new. Name, int new. Age, double new. Weight): void + get. Name(): String + get. Age(): int + get. Weight(): double
Another Overloading Example (cont’d) /** Class for basic pet records: name, age, and weight. */ public class Pet { private String name; private int age; //in years private double weight; //in pounds /** This main is just a demonstration program. */ public static void main(String[] args) { Pet my. Dog = new Pet( ); my. Dog. set("Fido", 2, 5. 5); my. Dog. write. Output( ); System. out. println("Changing name. "); my. Dog. set("Rex"); my. Dog. write. Output( ); System. out. println("Changing weight. "); my. Dog. set(6. 5); my. Dog. write. Output( ); System. out. println("Changing age. "); my. Dog. set(3); my. Dog. write. Output( ); }
Another Overloading Example (cont’d) public void write. Output( ) { System. out. println("Name: " + name); System. out. println("Age: " + age + " years"); System. out. println("Weight: " + weight + " pounds"); } public void set(String new. Name) { name = new. Name; //age and weight are unchanged. } public void set(int new. Age) { if (new. Age <= 0) { System. out. println("Error: invalid age. "); System. exit(0); } else age = new. Age; //name and weight are unchanged. }
Another Overloading Example (cont’d) public void set(double new. Weight) { if (new. Weight <= 0) { System. out. println("Error: invalid weight. "); System. exit(0); } else weight = new. Weight; //name and age are unchanged. } public void set(String new. Name, int new. Age, double new. Weight) { name = new. Name; if ((new. Age <= 0) || (new. Weight <= 0)) { System. out. println("Error: invalid age or weight. "); System. exit(0); } else { age = new. Age; weight = new. Weight; } }
Another Overloading Example (cont’d) public String get. Name( ) { return name; } public int get. Age( ) { return age; } } public double get. Weight( ) { return weight; }
Overloading and Automatic Type Conversion o Java always tries to use overloading before it tries to use automatic type conversion. o If it can find a method definition that matches the argument types it will use that method. o Java will do automatic type conversion only if it fails to match the argument types.
The Money Class Example import java. util. *; /** Objects represent nonnegative amounts of money, such as $100, $41. 99, $0. 05. */ public class Money { private long dollars; private long cents; public void set(long new. Dollars) { if (new. Dollars < 0) { System. out. println( "Error: Negative amounts of money are not allowed. "); System. exit(0); } else { dollars = new. Dollars; cents = 0; } }
The Money Class Example (cont’d) public void set(double amount) { if (amount < 0) { System. out. println( "Error: Negative amounts of money are not allowed. "); System. exit(0); } else { long all. Cents = Math. round(amount*100); dollars = all. Cents/100; cents = all. Cents%100; } } public void set(Money other. Object) { this. dollars = other. Object. dollars; this. cents = other. Object. cents; }
The Money Class Example (cont’d) /** Precondition: The argument is an ordinary representation of an amount of money, with or without a dollar sign. Fractions of a cent are not allowed. */ public void set(String amount. String) { String dollars. String; String cents. String; if (amount. String. char. At(0) == '$') //Delete '$' if any amount. String = amount. String. substring(1); amount. String = amount. String. trim( ); int point. Location = amount. String. index. Of(". "); //Locate decimal point if (point. Location < 0) //If no decimal point { cents = 0; dollars = Long. parse. Long(amount. String); } else //String has a decimal point. { dollars. String = amount. String. substring(0, point. Location); cents. String = amount. String. substring(point. Location + 1); if (cents. String. length( ) <= 1) //if one digit meaning tenths of a dollar cents. String = cents. String + "0"; dollars = Long. parse. Long(dollars. String); cents = Long. parse. Long(cents. String); if ((dollars < 0) || (cents > 99)) { System. out. println( "Error: Illegal representation of money. "); System. exit(0); } } }
The Money Class Example (cont’d) public void read. Input( ) { System. out. println("Enter amount on a line by itself: "); Scanner keyboard = new Scanner(System. in); String amount = keyboard. next. Line( ); set(amount. trim( )); } /** Does not go to the next line after outputting money. */ public void write. Output( ) { System. out. print("$" + dollars); if (cents < 10) System. out. print(". 0" + cents); else System. out. print(". " + cents); }
The Money Class Example (cont’d) /** } Returns n times the calling object. */ public Money times(int n) { Money product = new Money( ); product. cents = n*cents; long carry. Dollars = product. cents/100; product. cents = product. cents%100; product. dollars = n*dollars + carry. Dollars; return product; } /** Returns the sum of the calling object and the argument. */ public Money add(Money other. Amount) { Money sum = new Money( ); sum. cents = this. cents + other. Amount. cents; long carry. Dollars = sum. cents/100; sum. cents = sum. cents%100; sum. dollars = this. dollars + other. Amount. dollars + carry. Dollars; return sum; }
Example of Using the Money Class public class Money. Demo { public static void main(String[] args) { Money start = new Money( ); Money goal = new Money( ); System. out. println("Enter your current savings: "); start. read. Input( ); goal = start. times(2); System. out. print( "If you double that, you will have "); goal. write. Output( ); System. out. println(", or better yet: "); goal = start. add(goal); System. out. println( "If you triple that original amount, you will have: "); goal. write. Output( ); System. out. println("Remember: A penny saved"); System. out. println("is a penny earned. "); } }
- Slides: 17