previous |
start |
next
Polymorphism
- Generic method:
public void transfer(double amount, BankAccount other)
{
withdraw(amount);
other.deposit(amount);
}
- Works with any kind of bank account (plain, checking,
savings)
- Subclass object reference converted to superclass reference
other
momsAccount.transfer(1000, harrysChecking);
- Note polymorphism:
other.deposit(amount)
calls CheckingAccount.deposit (and charges
transaction fee)
- Why not just declare parameter as Object?
- Object class doesn't have deposit method
previous |
start |
next