Lecture 5

Object-oriented Programming

Subclasses and inheritance

MCS 275 Spring 2024
Emily Dumas

View as:   Presentation   ·   PDF-exportable  ·   Printable

Lecture 5: Subclasses and inheritance

Reminders and announcements:

  • Homework 2 posted, submission opens soon. Due Tue at Noon.
  • Projects 1 and 2 coming up surprisingly soon:
    • Project 1 due Feb 9
    • Project 2 due Feb 23

Improved Point2 and Vector2

I polished plane.py a bit, and may soon add some test code.

You'll look at this more in worksheet 3.

Value of Point2 and Vector2

It is possible to do arithmetic with points and vectors.

It is impossible to perform meaningless arithmetic.
(e.g. point+point)

Photo by Mike Gogulski (CC-BY-SA)

Inheritance

Instead of defining a class from scratch we can have it inherit all the methods and attributes of some other class. Then we can add or change anything we like.

If new class B inherits from A in this way, we say:

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

Uses of inheritance

Some common reasons:

  • To change behavior of a built-in class
    (e.g. a dict that only allows certain kinds of keys)
  • To avoid duplication (several subclasses share one superclass)
  • To formalize relationships between classes

Meaning of inheritance

Subclassing should express an "is-a" relationship.

Dog and Cat might be subclasses of Pet.

Vector2 and Point2 might be subclasses of ObjectCartesianCoordinates2.

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 ...

Now, all the methods of the superclass are immediately available as part of self.

Class hierarchies

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

Lecture project

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 active, and method update() to advance time.

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 deactivates.

Robot simulation template

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

  • A start on module bots containing one class Bot. It 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.

Time to start building bots.

Method calls

Suppose B inherits from A.

In a method of B, calling self.f() will look for a method B.f, and if none is found it will call A.f.

Usually that's all you need.

super()

But what if B.f and A.f both exist, but in a method of B you really need to call A.f?

You can, with super().f().

Only use it for this special situation.

from

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

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 2021 Lecture 27
  • See Lutz, Chapter 31 for more discussion of inheritance.
  • Lutz, Chapters 26-32 discuss object-oriented programming.

Revision history

  • 2023-01-25 Finalization of the 2023 lecture this was based on
  • 2024-01-19 Initial publication.