Python Programming: Class Inheritance


Learning Objectives

After this lesson, you will be able to…

  • Implement inheritance.
  • Describe what has been inherited from one class to another.
  • Overwrite variables and methods.

Discussion: Similar Classes

Phone is a class — there are hundreds of types of phones.

  • What attributes and functions would a Phone have?

What about an iPhone? Or android_phone?

  • iPhone and android_phone would be objects of the Phone class.
  • But, there are different types of iPhones and Android phones.
  • Should IPhone and AndroidPhone be classes themselves?

What would you do?


Introduction: Inheritance

AndroidPhone and IPhone are separate classes and in the Phone class.

This is called inheritance: making classes that are subsets of other classes.

Phone is the parent class. It’s a regular class! All phones:

  • Have a phone number.
  • Can place phone calls.
  • Can send text messages.

IPhone is a child class. The child class inherits methods and properties from the parent class but can also define its own functionality. iPhones uniquely:

  • Have an unlock method that accepts a fingerprint.
  • Have a set_fingerprint method that accepts a fingerprint.

We Do: Inheritance

All phones have a phone number, can place phone calls, and can send text messages.

Start a new file, Phone.py. In it, let’s start and test the class:


We Do: IPhone Class

Underneath the Phone class definition, let’s create the IPhone class.


We Do: IPhone Class

iPhones uniquely:

  • Have an unlock method that accepts a fingerprint.
  • Have a set_fingerprint method that accepts a fingerprint.

Side Discussion: Edge Cases

Look at:

What if self.fingerprint is currently None? We need to account for this!

When programming, always watch for edge cases. This isn’t specific to classes!


We Do: Testing IPhone

Add some test lines at the bottom:

Try it! Then, try this. Why does it fail?


Quick Recap: Inheritance

  • A class can inherit from another class — a parent class and a child class.
  • The child class can declare its own variables and methods, but it also has access to all the parents’.

I Do: Overwriting Attributes

Next up: Overwriting attributes!

Let’s switch to a new example. You don’t need to follow along.

Here’s a regular Building class:


I Do: Inheriting Building

Inheriting from Building, we can create a Mansion class.


Overwriting Variables

What if we want the class variables to have different values? We can set new ones. Remember, child classes do not affect the parent class.


Discussion: Child Class Methods

In the Building class, we have:

What if a Mansion’s price calculation is different? What do you think we can do?


Overwriting Methods

We know that we can overwrite variables. Turns out, we can also overwrite methods!


Quick Review

When we make child classes, we can overwrite class variables and methods.


Knowledge Check

Consider the following classes:

You instantiate two objects: bug = Grasshopper() and cat = Animal(). Which of the following instance methods are available for each?

  1. bug.is_mammal()
  2. bug.is_alive()
  3. bug.is_small()
  4. bug.is_animal()

Summary and Q&A

Inheritance:

  • Allows us to make classes using other classes as templates.
  • Has a parent class (Phone) and a child class (IPhone).
    • The parent class is still a usable class!

Child classes:

  • inherit methods and properties from a parent class.
  • Have access to all of the functionality of its parent.
  • Can have new attributes and methods.
    • They won’t be available to the parent.
  • Can overwrite values from the parent class.