Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In object-oriented programming, inheritance is when one class inherits the properties of another class. The class that is being inherited from is often called a base class or a parent class. Let's take a look at how this is done in Python. We'll start by making a working copy of classes.py, classes-working.py, and we'll open our working copy. You notice that we have this Duck class here. We're going to create another class. We'll call this one Animal.
So the Animal class will have methods in it that will be inherited by the Duck class and perhaps other animals as well. We'll start by saying that an animal makes a sound, so we'll call talk and that will print 'I have something to say!', and say that the animal moves around. We'll call that walk, print. 'Hey! I' 'm walkin'' here!' And then an animal is covered with something, usually fur or feathers or something. We'll call that clothes.
'I have nice clothes'. [00:01:22.02 All right. So there is our animal. Now, Duck can inherit all of these methods from Animal, in fact all of its attributes, by simply saying Animal here. Putting in a parenthesis in the definition of Duck. Now, Duck is said to be, is an animal. Duck is a animal. Object-oriented speak for saying that the Duck inherits the properties of Animal.
So Duck will continue to work the same way that it did before. If we save this and we run it, we don't really see any difference in how it works. Except that now it has access to these other properties. I can say donald.clothes and save that and run it. And we see donald says he has nice clothes. You'll notice that walk printed "Walks like a duck." It did not print "Hey! I'm walkin' here!" That's because walk in Duck overrides walk in Animal.
The way that works is that Duck inherits Animal and then Duck defines a method called walk, so it uses this one instead of the one in its parent. If we wanted to say incorporate the one from its parent, we can do that with the super function. This is a built-in function which accesses the parent class and just say, super().walk() like that, and now donald will do both. Save that and run it, and he says, "Hey! Im walkin here!" And he says, "Walks like a duck." So this is how you can access the class in the parent, even though you're overriding it in the class that inherits from the parent.
So what makes this useful is that now we've defined an animal base class and now if we want to have another animal, it's very easy for us to do that. We can say class Dog and it inherits from Animal, and that's really all that we need to define. We can just say pass here and we can say fido = Dog(), and fido.walk(), and we have a working dog.
Save that and run it. And there we have, "Hey! I'm walkin' here!" And that's from fido. So obviously a dog, instead of having clothes, he is got fur. So we can say, def clothes, print, 'I have brown and white fur'. And then down here if we say fido.clothes(), save that and run it, and it says, "I have brown and white fur." But what we've done is we've created a base class that perhaps has all the properties that are going to be common to the classes that we're creating and then we simply inherit that and that makes our code very reusable.
You'll see this technique used a lot. In fact, in an object-oriented language like Python, virtually all of the classes at some level are inheriting form other classes. You have two classes like the tuple and the list and they do a lot of common similar things. In fact, strings do as well. And so those are all inheriting from some common classes and simply building upon that and overwriting some methods and implementing some and not implementing others.
And that allows them to reuse a lot of code and it also allows them to operate in a lot of the same contexts as each other. So this is inheritance and this is how you do inheritance in Python.
Get unlimited access to all courses for just $25/month.Become a member