Get startedGet started for free

Multilevel inheritance

1. Multilevel inheritance

Great job on creating the SocialMedia class for your package. Thanks to the Document class and inheritance you were able to quickly build up the class for a more specific task, but what if we wanted to go even more specific.

2. Creating a Tweet class

The SocialMedia class has some general functionality dealing with hashtags and mentions that works across multiple social media platforms. If we wanted to include functionality about retweets, it wouldn't be appropriate to include in the general class; instead, we can create a Tweet class. So how can we do this without losing the benefits of both the Document and SocialMedia classes? With inheritance again of course!

3. Multilevel inheritance

We've seen before that a child class can inherit from a parent class. We can continue the family tree again with inheritance. With multilevel inheritance, our child class is all grown up and can its functionality onto its children.

4. Multilevel inheritance

Thanks to inheritance we pass along the traits of all prior classes in the family tree. Note, that in the graphic we only have one inheriting class at each level, but we are by no means limited to this. Multiple classes can inherit from the same parent.

5. Multiple inheritance

In fact, one child class can inherit from multiple parents. This more advanced OOP concept is known as 'multiple inheritance'. We won't be covering it in this course, but it's good to familiarize yourself with the definition in case you hear of it out in the wild.

6. Multilevel inheritance and super

Let's code up an example of multilevel inheritance. We'll start with the inheritance pattern we've seen before. Here we define a Parent and Child class that inherits from the Parent. We could do this differently, by using the super function. Instead, of directly calling the __init__ method of Parent we use the super function. This makes no functional difference in our code here but it has some advantages in maintainability and when implementing multiple inheritance. However, there are some 'gotchas' that can arise with super and multiple inheritance; you can check out this companion DataCamp tutorial to learn more about it.

7. Multilevel inheritance and super

Let's continue the family tree, to create a Grandchild class that inherits from SuperChild we use the same super syntax in its __init__ method, and voila. If we create an instance of Grandchild we can see from the output that each __init__ method in the family tree was called.

8. Keeping track of inherited attributes

If you're using inheritance it's sometimes hard to remember what attributes and methods your class has at the end of it all. If you're using an IDE, then generally you can use tab complete to get a list of suggestions. However, if you want to get the info from the console, you can use either help or the dir function. help is a good option in most cases, but it will only include public methods in it's output. Using dir will print a fairly exhaustive list of what your class has under the covers. The list includes methods that come with our class object by default. Near the end of the list, you see all the methods and attributes that you personally programmed into the SocialMedia class. dir can definitely come in handy, but a warning from the documentation, "dir is supplied primarily as a convenience for use at an interactive prompt." So it's not advised to use it in your scripts.

9. Let's Practice

We just learned more about how powerful inheritance can be. In the exercises, you'll use multilevel inheritance to create a new Tweet class.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.