Inheritance
The bank wants a savings account. It behaves exactly like the account you already built — same owner, same balance, same deposit — plus interest.
You could copy the class and add a method. Then every fix to withdraw has to be
made twice, and one day it won't be. Inheritance says it differently: a
savings account is an account, with one addition.
A subclass writes down only the difference
SavingsAccount never defines __init__ or deposit — it gets both. The
parent goes in the parentheses, and that's the whole declaration.
Extending the parent's setup with super()
When the child needs its own state too, call up to the parent first:
Skip that super() call and the parent's setup never runs — so the attributes it
would have created simply don't exist:
AttributeErrorRun it and read the error — it's telling you an attribute is missing. Then make it print 0.
Overriding: replace, or build on
A child can redefine a method outright, or reuse the parent's result:
super().summary() runs the parent's version and then decorates it — so an
improvement upstream still reaches the child.
A first taste: one loop, many types
Here's the payoff, and it has a name — polymorphism, the next lesson's whole subject. This loop never asks what kind of account it's holding:
Adding a third product means writing a third class. The loop doesn't change — that's the property worth having, and it's why "which type is this?" checks are a smell in object code.
Python takes this further than most languages: it never checked that either type
inherited from anything. It only needed monthly() to exist.
class Duck:
def speak(self): return "Quack"
class Robot:
def speak(self): return "Beep"
print(" ".join(x.speak() for x in [Duck(), Robot()]))Watch the two monthly() calls resolve to different code from the same line:
🎯 Your turn
Define Account with deposit(amount), then SavingsAccount(Account) adding a
rate and add_interest(). Write savings_after(start, rate, years) applying
interest once per year and returning the balance rounded to two decimals:
savings_after(100, 0.1, 2)→121.0savings_after(100, 0.1, 0)→100.0
Hint — super().__init__(owner, balance) first, then self.rate. Round only
at the very end.
Then press ▶ Run and hit ✓ Check. ✅
