From the course: Python Object-Oriented Programming

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Understanding composition

Understanding composition

- [Instructor] Earlier in the chapter, we learned about how Python implements the concept of inheritance in order to create class hierarchies. In this example, we'll see how we can use a concept called composition to create complex objects out of simpler ones. You might recall that inheritance is an is type of relationship. Here in this class diagram, a book is a publication because it inherits from the publication base class and picks up all the attributes and methods from that base class. Composition works a little bit differently. When using composition, we build objects out of other objects, and this model's more of a has relationship than an is relationship. So in the diagram on the right, the book object has an author object which contains information about the author rather than defining all of the author-related information directly in the Book class. This type of model lets us extract distinct ideas and…

Contents