Lecture 5

Object-oriented Programming

Subclasses and inheritance

MCS 275 Spring 2021
David Dumas

Lecture 5: Subclasses and inheritance

Course bulletins:
  • Worksheet 2 solutions available.
  • Quiz 2 to be posted at Noon CST Monday, due at Noon CST Tuesday.

Improved Point and Vector

I added new features to our plane module between lectures. Let's take a tour of the changes:

  • Point-Point gives displacement Vector
  • Can multiply Vector by integer or float
  • abs(Vector) gives length
  • Point and Vector support equality testing

(There are other features we might want in a real-world application, but this will suffice for now.)

Photo by Mike Gogulski (CC-BY-SA)

Inheritance

It is possible to build a class that is derived from an existing one, so that the new class inherits all the methods and behavior of the existing class, but can add new features, too.

If new class B is derived from existing class A in this way, we say:

  • B is a subclass of A (or child of A or inherits from A)
  • A is a superclass of B (or parent of B)

Why subclass?

Some common reasons:

  • To add custom behavior to an existing class
    (e.g. a dict that only allows certain kinds of keys)
  • To avoid code duplication when multiple classes share some behavior
  • To formalize relationships between classes

Subclassing should express an "is-a" relationship. Dog and Cat might be subclasses of Pet.

Python subclass syntax

Specify a class name to inherit from in the class definition:

    class ClassName(SuperClassName):
    """Docstring of the subclass"""
    # ... subclass contents go here ...

There is a built-in class object that every class inherits from, even if you don't specify it explicitly.

Class hierarchies

Inheritance patterns are often shown in diagrams. Lines represent inheritance, with the superclass appearing above the subclass (usually).

Live coding

Let's build a class hierarchy for a simple robot simulation.

Every type of robot will be a subclass of Bot.

Bot has a position (a Point), boolean attribute alive, and method update() to advance one time step.

Subclasses give the robot behavior (e.g. movement).

Planned Bot hierarchy

  • PatrolBot walks back and forth.
  • WanderBot walks about randomly.
  • DestructBot sits in one place for a while and then self-destructs.

Robot simulation template

We haven't built any of the Bot subclasses yet, but I have already created:

  • A barebones module bots containing a class Bot. This robot sits in one place. In bots.py in the sample code repository.
  • A script botsimulation.py to run the simulation and show it with simple text-based graphics.

super()

In methods of a subclass, super() returns a version of self that behaves like an instance of the superclass.

super() allows the subclass to call methods of the superclass even if the subclass overrides them.

from

The from keyword can be used to import individual symbols from a module into the global scope.

So

import mymodule
# ...
mymodule.useful_function()  # module name needed
is equivalent to
from mymodule import useful_function
# ...
useful_function()  # no module name needed

Please use from very sparingly!

References

  • I discussed inheritance in MCS 260 Fall 2020 Lecture 25, using "Square is a subclass of Rectangle" as an example in this geometric object module.
  • See Lutz, Chapter 31 for more discussion of inheritance.
  • Lutz, Chapters 26-32 discuss object-oriented programming.

Revision history

  • 2021-01-22 Indicate worksheet 2 solutions posted; fix typo
  • 2021-01-22 Initial publication