• 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 1/26/21

Write a Subclass in Python

Log in or subscribe for free to enjoy all this course has to offer!

Define the Subclassing Syntax

The main piece of syntax used for defining subclasses is in the class definition itself:  class Employee(Person) . The class’s parent is bracketed after the class’s name in the first line of the class. 

class TextContactSystem(ContactSystem):
def __init__(self, phone_number):
self.phone_number = phone_number
def send(self, message):
send_text_message(self.phone_number,message)

In the case of multiple inheritances - which we’ll briefly touch on later in Chapter 4 - the parent classes are separated by brackets, similar to the way parameters to functions work, like this:  class Subclass(ParentOne,ParentTwo)...

Your Turn: Write Code Using Inheritance

In this exercise, you’ll be using the classes you defined in the last chapter. In your development environment, write a class definition for each of those classes (using subclasses where relevant).

Assume that, for now, you’re just printing a post’s content to the terminal. We’ll deal with displaying threads later, so just use  pass  for that method for now.

When you don’t know how to implement a method, you can use  pass  as that method’s body. Use your judgment when implementing these methods. Remember to include constructors for each class!

Practice Makes Perfect

Complete the following activity to reinforce your new skills! 😸🐯🦁

Coding application
Houston... ?
It seems you're not connected to the internet.
You are not signed in

Sign in to access coding exercises and test your new skills.

Make sure to practice in this chapter before moving on.

Let’s Recap!

  • Define a child class using  class Child(Parent).

  • By default, all classes inherit from an object - a Python object that provides basic functionality.

  • It’s possible for classes to inherit from multiple parent classes at once - in multiple inheritance.

Now that you have your subclasses, join us in the next chapter, where we use overriding to allow subclasses to have different behavior than their parents.

Example of certificate of achievement
Example of certificate of achievement