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_numberdef 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:
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! 😸🐯🦁
You are not signed in
Sign in to access coding exercises and test your new skills.
Define a child class using
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.