Introduction to OOP Inheritance
In this video, you’ll learn how inheritance is used to write maintainable Python code that avoids redundancy.
Inheritance allows one class to subclass another. If we want to make the
Baby class a child class of the
Person class, we can define
Baby like this:
class Baby(Person): # Baby code here
By default, any
Baby object we create will inherit its attributes and methods from its parent. This means it will have the same attributes and members that the parent class defined.
In the video, you’ll see how you can override a method so that the
Baby class can contain a different implementation for that method. You’ll also learn how to extend the functionality of the
Baby class by defining new methods, which will be exclusive to only
isinstance() function is used to determine if an object is of a specific type. It takes 2 arguments: the object to be checked, and a type to check against. It supports inheritance, so in the example of our
isinstance(baby, Person) will return
True because our
Baby class is the child class of
Person, and so we can say our
baby object is a Person too.
00:00 Welcome back to our series on object-oriented programming in Python. In the last video, we learned about instance methods, which are used to represent the behaviors our objects have. This whole idea of classes, creating objects—little bundles of associated data and actions—is a really powerful thing, but it’s also susceptible to a bad coding practice called DRY. DRY stands for Don’t Repeat Yourself. This says that when we write code, we should avoid copying and pasting the same chunks of code whenever possible.
00:33 If you’re writing a nontrivial Python program and you find yourself copying and pasting attributes and instance methods between a lot of different classes, that means that it’s probably time to rethink how you’re designing your software. Take a look at this example.
00:48 Let’s say we are writing a program that’s supposed to construct and maintain lots of different people. As you can probably guess, each person is going to be an object—but here’s the caveat: we can have general people objects, and then we can have a special type of person called a baby.
01:06 A baby can do everything a regular person can do, except it might do some things a little bit differently, like talking. In addition, it might also have some unique behaviors that our general person doesn’t have, such as the ability to take a nap whenever it wants. Being a baby sounds great.
As you might have guessed, we’re going to need two different classes here: one for the person, and then one for the baby. You’ll notice here that both of these classes are the exact same, but the
Baby class has one more behavior called
Now, we could design our software this way and it would work fine, but remember, a baby has to be able to do everything a more general person can do, albeit a little bit differently sometimes. This means that our
Baby class is going to have the same instance methods that the
Person class has, and most of those methods are going to look the exact same,
But now, what happens if we want to make a change to the
Person class? If we change the
.action()` method from something like jumping to spinning, then we also have to change the baby’s
.action() method as well.
And if we add other classes, like
Teenager, then we’d also have to maintain those, too. Copying code might make things easier in the moment, but it can make our software really difficult to maintain later on, especially as it grows. To solve this problem, let’s take a step back and think about the relationships that exist between these two classes.
I think that you would agree that a baby is a person, right? That’s why it shares the same behaviors as the
Person class. So, what if there was a way that we can make the
Baby class automatically inherit every attribute and instance method from the
Person, but then modify them a little bit to fit the
Baby class’ needs.
Then, we could even add our own behavior, like
.nap(), that would be exclusive to the
Baby class. This right here is called object inheritance, and if used right, it makes managing lots of similar classes a lot easier.
03:14 Inheritance allows us to make one class the child class, or the derived class, of another class called the parent class, or the base class. In Python, the child class automatically inherits every attribute and instance method from the parent class, but we can also redefine certain attributes or methods.
So, if we want our
Baby object to speak differently than a
Person object would, we can do that. If we want our
Baby object to have the ability to nap, we can do that too. And if we need to make our person spin, instead of jump, we can modify the
Person class and the
Baby class will automatically pick up those changes.
many languages, Python included,
object is the parent class of all classes in the language. This includes built-in classes, like the
int or the
str, as well as any custom classes we create. In this diagram, you can see that
Baby inherits from
Person, which in turn inherits from
Become a Member to join the conversation.