From the course: Python Object-Oriented Programming
Understanding inheritance - Python Tutorial
From the course: Python Object-Oriented Programming
Understanding inheritance
- [Instructor] One of the core concepts of object-oriented programming is the notion of inheritance. And in this example, we're going to see how that works in Python. Inheritance defines a way for a given class to inherit attributes and methods from one or more super classes. So this makes it easy to centralize common functionality and data in one place instead of having it spread out and duplicated across multiple classes. So let's open up the inheritance_start file. And in this example, you can see I have three classes, there's a Book, Magazine, and Newspaper, and each of one of these classes represents a type of Publication, and each of them has a set of attributes that are relevant to that Publication type. So Books have a title and a price along with the author's name and the number of pages. A Newspaper also has a title and a price, but it has a publisher instead of a single author and it has a Publication period because they're published on a Periodical basis. Magazines are the same, title, price, and they also have a Publication period and a publisher. And you can also see further down in the file, we have some code to create a few sample objects and then access some data on them. So before we do anything else, let's just go ahead and open up the terminal. And let's run what we currently have. And you can see that, when we do this, I'm printing out the author of Book one, the publisher of Newspaper one, and then the price of each, okay. And so at the moment, each of these classes is a standalone implementation, so let's hide the terminal. You can see that each one is its own class, but there's a considerable amount of duplication among the data that each class holds. So for example, all three classes have attributes for title and price, and the Newspaper and Magazine classes also have the same attributes for period and publisher. We can improve the organization of these classes and make it easier to introduce new classes by implementing some inheritance and class hierarchy. So let's start with the most obvious duplication, which is the title and price attributes. So what we could do is define a class named Publication. So let's make a class called Publication. Okay. And then have that class define those common attributes. So we'll put init here and we'll have the Publication class contain the title and price. And so that means that, on this object, we have to set title equal to title, oops, and self.price equal to price. All right. So now we can fix the Book class and have it inherit from the Publication class. To do that, inside these parentheses, I'm going to write Publication. Okay, and then we initialize the super class using the super function. So I'm going to create super and that's a function. And then I'm going to call the init with title and price. And Book no longer has to define title and price in its class because they're now defined in Publication. So I only need to keep the author and pages. Now, we could do the same thing with Newspaper and Magazine classes, but there's some duplication there too. So both of these classes have period and publisher attributes, so that's a pretty good hint that we can collect those in a super class also. So I'm going to create another class named Periodical. And Periodical is going to inherit from Publication. And of course, we'll have to create the init for Periodical as well. And that will take self, title, price, period, and publisher. And we'll call the super class for title and price. And then we'll have the Periodical class to find the period and the publisher. All right, so now we have a class hierarchy with Publication at the top, which Book inherits from. Then Periodical, which inherits from Publication, and then we're going to change Newspaper and Magazine to inherit from Periodical. So Magazine will inherit from Periodical and so will Newspaper. And then in the init function for each, we call the super class. And remember, in the case of Periodical, it takes all of these so that it's going to be, let's see. That's going to be title, price, period, and publisher. There we go. And now we can take off the ones here and we can copy this and paste it here and also take off this guy. All right. Okay, so now we should be able to run our original code, which we haven't touched, notice, right, we're still creating the Book, Newspaper, Magazine the same way. But what we've done is we've gathered all of the duplicated code into super classes so that each one of these classes, Book, Magazine, Newspaper, can take advantage of the fact that they already exist in a super class. So let's bring the terminal back up and let's run again. And sure enough, you can see we get the same output as we had before. So we're getting the same results, but with much better code organization, which is one of the main benefits of inheritance. I can now add properties that are specific to each kind of Publication in just one place. And I only have one place to edit them if I want to change the names of any of these attributes.