Skip to content

Inheritance

Write a subclass that inherits from BankAccount, calls the parent with super(), overrides a method, and say which method runs for each object.

It opens on

SavingsAccount is written as class SavingsAccount(BankAccount) and has only a method add_bonus, with no deposit. Does s.deposit(10) raise an error or change s.balance?

Why it matters

Real programs share code between kinds of things, such as a savings account that is a bank account with extras, and the questions that matter are which method Python runs for a given object and what breaks when the parent's setup is skipped; both are standard exam traces.

Good to know first

  • Methods, self and __str__: a call a.deposit(30) runs deposit with self set to a
  • Classes, objects and __init__: __init__ stores attributes in the new object
  • Aliasing and mutability: a name such as x in a loop points at an object

Step by step

  1. A subclass inherits

    Read a class written with a parent in brackets, and say which methods and attributes its objects have without the subclass writing them.

    Line 5 makes a SavingsAccount. The subclass has no __init__ of its own, so Python uses the parent's, which stores the owner Sam and the balance 200.

    A common mix-up

    A subclass has only the methods written in it.

    In fact: It has its own methods and all its parent's, found by looking up the class chain.

  2. The parent's __init__ and super()

    Write a subclass __init__ that calls super().__init__ and stores its own extra attribute, and say what breaks when the call is left out.

    Line 7 runs the subclass __init__ with self as the new object. Line 3 calls the parent's __init__, which stores owner Sam and balance 200, and line 4 stores the bonus 5. The run printed Sam 200 5.

    A common mix-up

    A subclass __init__ adds to the parent's __init__.

    In fact: It replaces it for that class, so the parent's lines run only if super().__init__ is called.

  3. Overriding and tracing

    Write a subclass method with the parent's name that overrides it, call the parent's version with super(), and trace which method runs for each object.

    On the first pass x points to a, which is a BankAccount. Its class has no override, so BankAccount.deposit runs and adds 10, which makes the balance 110.

    the first row of the table

    A common mix-up

    x.deposit(10) runs the same code for every x.

    In fact: Python picks the method by the class of the object x points to, so a SavingsAccount runs its own version.

  4. isinstance

    Use isinstance to ask whether an object is of a class or of a subclass of it, and tell it from comparing types exactly.

    The first test, isinstance(s, SavingsAccount), is True, since s was made from exactly that class.

    A common mix-up

    Isinstance(a, SavingsAccount) is True when SavingsAccount inherits from the class of a.

    In fact: The relation runs upward only, so a plain parent object is not an instance of its subclass.

Try it

With t = SavingsAccount("Tia", 50), t.deposit(5) and t.add_bonus(), what does t.balance show?Answer

60

Hint: Deposit is inherited, and the bonus adds 5

A subclass Fish(Pet) has __init__(self, name, depth) with only self.depth = depth. After f = Fish("Nemo", 3), which line fails: print(f.depth) or print(f.name)?Answer

Print(f.name)

Hint: The parent's init stores the name

A Pet has sound returning "..." and intro returning self.name + " says " + self.sound(). A Dog(Pet) overrides sound to return "woof". What does Dog("Rex").intro() return?Answer

Rex says woof

Hint: Self.sound() starts the lookup in the object's class

With classes Pet and Dog(Pet), what do isinstance(Dog("Rex"), Pet) and isinstance(Pet("Tib"), Dog) give?Answer

True and False

Hint: Only subclass to parent counts

Line 6 is s.deposit(10), and SavingsAccount has no deposit. How does Python find one to run?Answer

It looks in SavingsAccount first, finds none, then looks in the parent BankAccount and runs that deposit with self set to s.

Inheritance

30 minutes every month. No card.