4 (6,7) - Inheritance and Polymorphism
4 (6,7) - Inheritance and Polymorphism
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
6
2. Creating a Subclass (2/6)
◼ Recall the BankAcct class in lecture #3
lect3/BankAcct.java
class BankAcct {
private int acctNum;
private double balance;
public BankAcct() { }
public BankAcct(int aNum, double bal) { ... }
public int getAcctNum() { ... }
public double getBalance() {... }
public boolean withdraw(double amount) { ... }
public void deposit(double amount) { ... }
public void print() { ... }
}
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
7
2. Creating a Subclass (3/6)
◼ Let’s define a SavingAcct class
◼ Basic information:
❑ Account number, balance
❑ Interest rate
New requirements
◼ Basic functionality:
❑ Withdraw, deposit
❑ Pay interest
◼ Compare with the basic bank account:
❑ Differences are highlighted above
❑ SavingAcct shares more than 50% of the code with BankAcct
◼ So, should we just cut and paste the code from BankAcct
to create SavingAcct?
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
8
2. Creating a Subclass (4/6)
◼ Duplicating code is undesirable (không mong
muốn) as it is hard to maintain
❑ Need to correct all copies if errors are found
❑ Need to update all copies if modifications are required
◼ Since the classes are logically unrelated if the
codes are separated:
❑ Code that works on one class cannot work on the other
◼ Compilation errors due to incompatible (không
tương thích) data types
◼ Hence, we should create SavingAcct as a
subclass of BankAcct
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
9
2. Creating a Subclass (5/6)
BankAcct.java
class BankAcct {
The “protected” keyword
protected int acctNum;
allows subclass to access
protected double balance;
the attributes directly
//Constructors and methods not shown
}
The “extends”
class SavingAcct extends BankAcct { keyword indicates
inheritance
protected double rate; // interest rate
+ getRate() + getAcctNum()
+ payInterest() + getBalance()
+ print() + withdraw()
+ deposit()
+ print()
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
11
2.1 Observations
◼ Inheritance greatly reduces the amount of
redundant coding
◼ In SavingAcct class,
❑ No definition of acctNum and balance
❑ No definition of withdraw() and deposit()
◼ Improve maintainability:
❑ Eg: If a method is modified in BankAcct class, no
changes are needed in SavingAcct class
◼ The code in BankAcct remains untouched
❑ Other programs that depend on BankAcct are
unaffected very important!
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
12
2.2 Constructors in Subclass
◼ Unlike normal methods, constructors are NOT
inherited
❑ You need to define constructor(s) for the subclass
class SavingAcct extends BankAcct {
protected double rate; // interest rate
public SavingAcct(int aNum, double bal, double rate){
acctNum = aNum;
balance = bal;
this.rate = rate;
}
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
13
2.3 The “super” Keyword
◼ The “super” keyword allows us to use the methods
(including constructors) in the superclass directly
◼ If you make use of superclass’ constructor, it must be the
first statement in the method body
class SavingAcct extends BankAcct {
protected double rate; // interest rate
public SavingAcct(int aNum, double bal, double rate){
super(aNum, bal); Using the constructor
this.rate = rate; in BankAcct class
}
sa1.print();
sa1.withdraw(50.0); Inherited method from BankAcct
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
15
2.5 Method Overriding (1/2)
◼ Sometimes we need to modify the inherited method:
❑ To change/extend the functionality
❑ As you already know, this is called method overriding
◼ In the SavingAcct class:
❑ The print() method inherited from BankAcct should be
modified to include the interest rate in output
◼ To override an inherited method:
❑ Simply recode the method in the subclass using the
same method header
❑ Method header refers to the name and parameters type
of the method (also known as method signature)
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
16
2.5 Method Overriding (2/2)
class SavingAcct extends BankAcct { SavingAcct.java
SavingAcct.java
class SavingAcct extends BankAcct {
. . .
To use the print()
public void print() { method from BankAcct
super.print();
System.out.printf("Interest: %.2f%%\n", getRate());
}
}
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
18
Exercise
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
23
5. “is-a” versus “has-a” (2/2)
◼ UML diagrams
class BankAcct {
...
}
SavingAcct BankAcct
class SavingAcct extends BankAcct {
...
}
Solid arrow
Inheritance: SavingAcct IS-A BankAcct
class BankAcct {
...
}; Person BankAcct
class Person {
private BankAcct myAcct;
}; Dotted arrow
Attribute: Person HAS-A BankAcct
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
24
6. Preventing Inheritance (“final”)
◼ Sometimes, we want to prevent inheritance by
another class (eg: to prevent a subclass from corrupting the
behaviour of its superclass)
◼ Use the final keyword
❑ Eg: final class SavingAcct will prevent a subclass to be
created from SavingAcct
◼ Sometimes, we want a class to be inheritable, but
want to prevent some of its methods to be overridden
by its subclass
❑ Use the final keyword on the particular method:
public final void payInterest() { … }
will prevent the subclass of SavingAcct from overriding
payInterest()
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
25
Exercise
◼ Create 3 classes:
Person, Student and
Undergraduate class
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
27
Polymorphism
◼ Consider an array of Person
Person[] people = new Person[4];
people[0] = new
Student("DeBanque, Robin",
8812);
people[1] = new
Undergraduate("Cotty, Manny",
8812, 1);
[CMP167
[501043 Lecture
- Unit 5-Inheritance
5: Inheritance]
and Polymorphism]
35
End of file