• 12 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 4/18/24

Apply Inheritance in Python Code

What Is Inheritance?

One of the most useful things about object-oriented programming is the concept of inheritance. Inheritance allows you to create a subclass (or child) of a class that contains the parent’s attributes and other attributes specific to the child.

Here is an example of two classes: Person and Employee. Employee is a subclass/child of Person, and Person is a superclass/parent of Employee. 

Upper left, a diagram of a person, labeled Person, with the state
The superclass Person is the parent of the subclass Employee.

Notice that Walk() and Name are attributes of Person that are inherited by Employee, which also adds its own attributes: EmployeeID and Work().

This kind of relationship comes up a lot in the real world - can you think of another example?

Why Inheritance?

There are a few different interrelated reasons why we use inheritance in object-oriented programming:

Reusability

The simplest reason is the same reason that you write functions - reusability.

When you make Employee a subclass of Person, you don’t have to re-add the name attribute or re-write the Walk() Method.

Imagine you had a dozen subclasses of Employee - that’s a lot of copy and pasted code without inheritance! If you wanted to make a change to Walk()’s functionality, you’d have to update every single subclass.

Extensibility

Inheritance also allows for extensibility - i.e., the possibility of extending a program’s functionality without having to modify existing code. 

Let’s say you provide two options to contact your users - by text message or by email:

On the left, the class ContactSystem. Written beneath it is the behavior
The ContactSystem class and two of its subclasses, TextContactSystem and EmailContractSystem.

As you can see, the ContactSystem class defines a behavior - Send(message) - and is subclassed byTextContactSystem and EmailContactSystem.

If a User contains a preferred contact medium - of type ContactSystem - you know you can call Send() on it and that they’ll be sent that particular message in their chosen way.

Not only that, let’s say you want to extend the functionality of your system by adding a new way of contacting your users by Post. If you create a subclass called PostContactSystem and implement it, you don’t have to change anything in your User class, or any of the code involved in the sending of messages!

Models

Finally, one of the advantages of inheritance is about how you design systems. Inheritance, and classes in general, let you express relationships between parts of your code.

Just as you combine data and code together in a package using classes, you bind classes that are conceptually related through inheritance. We’ll talk more about inheritance hierarchies and multiple levels of inheritance in Chapter 4.

Classes and inheritance are useful because they make systems easier to understand. They allow developers and development teams to build conceptual models of the behaviors and data in the system - which is increasingly more important in the maintenance and extending of large and complex projects.

Your Turn: Apply the Concepts of Inheritance

In Part 1, you did an exercise involving identifying classes in a problem description. You broke those classes down into their state and behavior on paper.

Below are a few lines describing a problem domain. From this description, identify classes, their state and behavior, and which classes are the child/parent classes of others.

You’re designing an online web forum. On this forum, users will be able to register and log-in, create new threads, and post in existing threads.

A thread has a title, its time of creation, and a corresponding collection of posts to that thread. Each post contains text, the user who posted them, and its time of posting. The system needs to display a thread, which is done by showing each thread’s posts, plus that thread’s metadata.

It’s also possible for users to attach files to posts. Assume there can be many types of files, but you’re primarily interested in image files. A post can have one file attached to it, which will change how it is displayed.

Finally, special users called moderators can edit a post to contain new content.

Classes
File - Image
User - Moderator
Post - FilePost
Thread
 

File

Image

State

name

size

State

image_size

Behavior

display()

Behavior

User

Moderator

State

username

password

State

Behavior

login()

post(thread, content)

make_thread(title, content)

Behavior

edit(post, content)

Post

FilePost

State

user

time_posted

content

State

file

Behavior

display()

Behavior

Thread

State

title

time_posted

posts

Behavior

display()

add_post(post)

Let’s Recap!

  • A class can have a parent class or superclass.

  • The class with a parent is known as a subclass or child.

  • That class inherits attributes from its parent.

  • The child class can implement extra attributes on top of that which it inherits.

Now you understand the ideas of inheritance, join us in the next chapter where we’ll write some subclasses.

Example of certificate of achievement
Example of certificate of achievement