Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Deciding Between Composition and Inheritance

Give Feedback

00:00 You learned about a ton of new use cases for inheritance and composition in this course. It might seem like you’re walking on stepping stones through a sea of lava trying not to pick the wrong one, but it’s not necessarily as clear-cut as you might think.

00:19 There are many cases where either approach would work fine, and others where it comes down to personal preference or design constraints. To help you decide, follow these guidelines when building your classes.

00:34 Remember: the more of this you can think through before you start coding, the less likely you are to run into issues later on.

00:44 One—use inheritance over composition in Python to model a clear is a relationship. First, justify the relationship between the derived class and its base.

00:57 Then, reverse the relationship and try to justify it. If you can justify the relationship in both directions, then you should not use inheritance between them. Two—use inheritance over composition to leverage both the interface and implementation of those base classes.

01:20 Three—use inheritance over composition to provide mixin features to several unrelated classes where there is only one implementation of that feature. This could be like a utility, such as a method to represent the object as a dictionary. Four—use composition over inheritance to model a has a relationship that leverages the implementation of the component class. Five—use composition over inheritance to create components that can be reused by multiple classes in your Python application.

02:04 Six—use composition over inheritance to implement groups of behaviors and policies that can be applied interchangeably to other classes to customize their behavior. Finally, number seven—use composition over inheritance to enable run-time behavior changes without affecting existing classes. Ultimately, the big idea here is to not fear failure.

02:35 This course showed you two new mechanisms you can use to build efficient class hierarchies. Inheritance and composition are a huge part of software architecture, a subject that can only be mastered with lots and lots of practice, and the best way to practice this is to plan out a project as best as you can, build it, and see how far you can get with your current design. It’s all right to realize halfway through coding that you probably should have used composition instead of inheritance, or vice versa.

03:11 Redesigning your software projects and building them—like we did many times in this course—will improve your understanding of these principles and you’ll see new ways to solve problems. It’s extremely rewarding, albeit a bit frustrating at times.

Become a Member to join the conversation.