Spaces:
Running
Running
same method, multiple param nums AND dtypes
Browse files
Week 5: Class hierarchies/10A. OVERLOADING AND OVERRIDING methods [= same method, multiple param nums AND dtypes]++
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
In Java, there can be SEVERAL VERSIONS of the SAME METHOD.
|
| 2 |
+
Methods are distinguished by formal PARAMETERS:
|
| 3 |
+
if two methods have different NUMBERS or TYPES of parameters,
|
| 4 |
+
both method definitions can co-EXIST in the SAME CLASS.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
For example, the following cases are therefore ALLOWED:
|
| 9 |
+
|
| 10 |
+
1. Different number of parameters
|
| 11 |
+
public int average(int a, int b)
|
| 12 |
+
public int average(int a, int b, int c)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
2. Different types of parameters
|
| 16 |
+
public int num(int a)
|
| 17 |
+
public int num(double a)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
3. Parameters in a different order
|
| 21 |
+
public int average(int a, String b)
|
| 22 |
+
public int average(String a, int b)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
Only the method name and its parameters are considered for OVERLOADING.
|
| 26 |
+
Changing the RETURN VALUE or the VISIBILITY WRAPPER DOES NOT OVERLOAD the METHOD,
|
| 27 |
+
but causes a translation error: 'Duplicate method x in type y'.
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
For example, the following method definition will cause a translation error:
|
| 32 |
+
public int average(int a, String b)
|
| 33 |
+
private int average(String a, int b) //change visibility. public -> private
|
| 34 |
+
public String average(int a, String b) //change return dtype. int -> String
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
Overloading also works without recovery.
|
| 41 |
+
As an example, consider the class 'MathHelper', which has two methods for calculating the average.
|
| 42 |
+
One is given 2 integers as parameters and the other 3 integers:
|
| 43 |
+
class MathHelper {
|
| 44 |
+
|
| 45 |
+
public double average(int num1, int num2) {
|
| 46 |
+
return (num1 + num2) / 2.0;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
public double average(int num1, int num2, int num3) {
|
| 50 |
+
return (num1 + num2 + num3) / 3.0;
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
Now, when calling a method, the Java COMPILER INFERS the CORRECT METHOD based on the actual parameters in the call.
|
| 59 |
+
This is sometimes called "STATIC (or COMPILE-TIME) POLYMORPHISM".
|
| 60 |
+
public static void main(String[] args) {
|
| 61 |
+
MathHelper helper = new MathHelper();
|
| 62 |
+
System.out.println(helper.average(1,2));
|
| 63 |
+
System.out.println(helper.average(1,2,3));
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
Program outputs:
|
| 67 |
+
1.5
|
| 68 |
+
2.0
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
=====================================================================
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
In inheritance, OVERRIDING is often a natural way to expand a child class.
|
| 79 |
+
For example, consider again the 'Account' class, which has the method deposit(int amount):
|
| 80 |
+
class Account {
|
| 81 |
+
protected double balance;
|
| 82 |
+
|
| 83 |
+
public Account(double balance) {
|
| 84 |
+
this.balance = balance;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
public double getBalance() {
|
| 88 |
+
return balance;
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
public void deposit(double amount) {
|
| 92 |
+
balance += amount;
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
The class 'BonusAccount' inherits the class 'Account',
|
| 98 |
+
and adds a new overrided version of the method 'deposit',
|
| 99 |
+
which also gets a 'bonus' added to its parameter:
|
| 100 |
+
class BonusAccount extends Account {
|
| 101 |
+
// CONSTRUCTOR
|
| 102 |
+
public BonusAccount(double balance) {
|
| 103 |
+
super(balance);
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
// OVERRIDED METHOD
|
| 107 |
+
public void deposit(double amount, double bonus) {
|
| 108 |
+
super.deposit(amount + bonus);
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
Now a client of the BonusAccount class can use BOTH the
|
| 118 |
+
OVERRIDED version of the method and the ORIGINAL version defined in the PARENT Account class:
|
| 119 |
+
|
| 120 |
+
public static void main(String[] args) {
|
| 121 |
+
BonusAccount acc = new BonusAccount(0);
|
| 122 |
+
acc.deposit(100);
|
| 123 |
+
System.out.println(acc.getBalance());
|
| 124 |
+
|
| 125 |
+
acc.deposit(100, 50);
|
| 126 |
+
System.out.println(acc.getBalance());
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
Program outputs:
|
| 131 |
+
100.0
|
| 132 |
+
250.0
|
| 133 |
+
|