[We hope that you can view the videos! However, if your internet connection is too slow for viewing videos, download the audio file (mp4) and the slides (pdf). Then you can listen to the audio as you go through the slides.]

Announcement

We are changing the order in which we'll present the next two topics. On April 30, we will discuss while loops, which corresponds to Sections 7.3 and 7.4 of the textbook. We will return to the topic of subclassing on May 5 (no required reading from the textbook).

Inheritance and Subclassing

We discuss one of the most important features of object-oriented programming, which is the ability to extend an existing class in order to get a new class that has behaviors modified from those of the existing class.

Slides for all 3 videos of this lecture: individual slides for viewing and a "6-up" version for printing.

The answers to the questions asked during this lecture are in a separate file at the end of this page.

Part 1: Defining subclasses

In this segment, we discuss the hierarchy for organizing the classes that one could write to support a drawing app: Shape, Circle, Rectangle, Square. We learn that a subclass (child class) inherits the instance attributes and methods of the superclass (parent class). In a subclass, we can override the parent's version of a method; at the same time we can use super() to access the parent's original version of a method.

Part 2: Design choices for method draw

Why and how do we divide up the task of drawing a shape between the parent and child classes? We make use of the Python module turtle for graphics. Note that you do not need to learn about turtle or graphics; we use it this lecture only to illustrate some design choices.

Part 3: Class attributes and various kinds of comparison

Like instance attributes and methods, class attributes can be inherited as well. We discuss the method __eq__, the operator is, and the function isinstance, all of which can be used for comparing an instance to something.

Answers to the questions asked during lecture: pdf