Inheriting From Other Classes
00:00 Another way to create relationships between classes is to use a mechanism called inheritance. Inheritance can be a tricky concept to wrap your head around or understand when it might be useful in a real-life situation. That said, it is a handy tool—maybe not as common as composition, but it’s still handy. For instance, inheritance can be really helpful in creating variations in classes.
00:39 Do you write the whole class again with just one different method? No. You can use inheritance to do that very cleanly. Inheritance also happens to be a favorite source of OOP interview questions, so it’s well worth being exposed to it.
For the example, you’re going to be working with this
Doggo class, which represents a dog. This
Doggo class has a class attribute, a constructor method, a special instance method, or a dunder method, and a normal method.
Then you can choose any of these doggos and get them to speak, and they all behave in exactly the same way. That’s to be expected. But maybe you want to define different breeds of
Doggo, and you want that to be obvious from the class name. Well, inheritance gives you a way to do that.
The way you define a subclass is you take the
class keyword, as you do with normal classes. You have the name—again, as you do with normal classes—but instead of going straight to the colon (
:) you add in some brackets (
()) as if it were a function.
02:14 Just a quick note on terminology here. When I say brackets, I mean round brackets or parentheses. If we come across square, curly, or angled brackets, I’ll explicitly say so. But if I just say brackets, I’m referring to round brackets or parentheses.
Now these names will come up whenever you instantiate an instance from these classes. Note that we’re just leaving these empty for now. The default behavior of this will be that since its subclassing
Doggo, these classes are going to behave exactly the same as the
03:41 This is so that it fits in the window without going off the edge. If you have two f-strings together, they will automatically concatenate. And the way you make sure that these are taken together is with the brackets, which is missing one here.
this is equivalent. Anyway, say you wanted to make another type of class with slight variations, let’s just say with the name of the class for now. So we can say
class Labrador()`, and using the brackets, within that, we put in the parent class.
True because a
Labrador is an instance of a
Doggo because it’s within that inheritance chain. And you could subclass
Labrador again into say,
LabradorRetriever, and the
LabradorRetriever would still be an instance of
Doggo because it shares that inheritance.
07:03 That’s an introduction to inheritance. You’ve seen how you can extend classes. That means having a parent class, which you then subclass. Note that you may not need inheritance. Composition is easier to get your head around, and for a lot of cases, it’s more flexible than inheritance.
Become a Member to join the conversation.