Suppose we have a BankAccount class:
class BankAccount {
var balance = 0.0
func deposit(amount: Double) {
balance += amount
}
func withdraw(amount: Double) {
balance -= amount
}
}
SavingsAccount
extends BankAccount
class
class SavingsAccount: BankAccount {
varinterest = 0.0
func addInterest() {
let interest = balance \*0.005
self. deposit(amount: interest)
}
}
The new SavingsAccount
class (subclass) automatically gets all the characteristics of the BankAccount
class (superclass). Additionally, the SavingsAccount
class defines an .interest
property and an .addInterest()
method.
Comments