Lesson 6 of 10

A class that inherits from another

Goal

Add your rule, leave the rest to the parent

01

Explanation

A child class inherits every method of its parent class, then adds to them or changes some of them. class SavingsAccount(Account) means a savings account is an account too: it deposits and withdraws like one, and any code that accepts an Account accepts it.

Write a method with the same name in the child and it overrides the parent's. But don't copy the parent's code into it. Add your rule, then call super().withdraw(amount) to let the parent finish the job, so the balance rule stays written in one place.

The best-known trap is that the child's __init__ replaces the parent's entirely. If you don't call super().__init__(owner), self.balance never exists, and the error doesn't appear when the object is created but at the first deposit, far from its real cause.

02

Your task

Finish SavingsAccount(owner, limit) so that it calls the parent's __init__ and stores limit, and overrides withdraw to raise ValueError when the amount is over limit, leaving the rest to the parent.

Checks

6 checks
  1. SavingsAccount inherits from Account
  2. Calls the parent's __init__, so the balance starts at zero
  3. Withdrawing over the limit raises ValueError and leaves the balance unchanged
  4. Withdrawing within the limit works
  5. The inherited balance rule still holds
  6. Account itself has no withdrawal limit
main.py
OutputPython (Pyodide)

Loading Python. This only happens once. Your browser keeps it after that.

–/6

Checks start once the environment is ready.

6 checks listed under “Your task”.