1
00:00:00,000 --> 00:00:03,990
In object-oriented programming, inheritance is when one class inherits the

2
00:00:04,000 --> 00:00:05,990
properties of another class.

3
00:00:06,000 --> 00:00:11,990
The class that is being inherited from is often called a base class or a parent class.

4
00:00:12,000 --> 00:00:13,990
Let's take a look at how this is done in Python.

5
00:00:14,000 --> 00:00:21,990
We'll start by making a working copy of classes.py, classes-working.py, and

6
00:00:22,000 --> 00:00:22,990
we'll open our working copy.

7
00:00:23,000 --> 00:00:26,990
You notice that we have this Duck class here.

8
00:00:27,000 --> 00:00:28,990
We're going to create another class.

9
00:00:29,000 --> 00:00:32,990
We'll call this one Animal.

10
00:00:33,000 --> 00:00:37,990
So the Animal class will have methods in it that will be inherited by the Duck

11
00:00:38,000 --> 00:00:40,990
class and perhaps other animals as well.

12
00:00:41,000 --> 00:00:46,990
We'll start by saying that an animal makes a sound, so we'll call talk and that

13
00:00:47,000 --> 00:00:57,990
will print 'I have something to say!', and say that the animal moves around.

14
00:00:58,000 --> 00:01:00,990
We'll call that walk, print. 'Hey!

15
00:01:01,000 --> 00:01:13,990
I' 'm walkin'' here!' And then an animal is covered with something, usually fur or

16
00:01:14,000 --> 00:01:17,990
feathers or something. We'll call that clothes.

17
00:01:18,000 --> 00:01:24,990
'I have nice clothes'. [00:01:22.02

18
00:01:25,000 --> 00:01:26,990
All right. So there is our animal.

19
00:01:27,000 --> 00:01:32,990
Now, Duck can inherit all of these methods from Animal, in fact all of its

20
00:01:33,000 --> 00:01:35,990
attributes, by simply saying Animal here.

21
00:01:36,000 --> 00:01:40,990
Putting in a parenthesis in the definition of Duck.

22
00:01:41,000 --> 00:01:45,990
Now, Duck is said to be, is an animal. Duck is a animal.

23
00:01:46,000 --> 00:01:51,990
Object-oriented speak for saying that the Duck inherits the properties of Animal.

24
00:01:52,000 --> 00:01:55,990
So Duck will continue to work the same way that it did before.

25
00:01:56,000 --> 00:02:00,990
If we save this and we run it, we don't really see any difference in how it works.

26
00:02:01,000 --> 00:02:03,990
Except that now it has access to these other properties.

27
00:02:04,000 --> 00:02:08,990
I can say donald.clothes and save that and run it.

28
00:02:09,000 --> 00:02:12,990
And we see donald says he has nice clothes.

29
00:02:13,000 --> 00:02:15,990
You'll notice that walk printed "Walks like a duck."

30
00:02:16,000 --> 00:02:18,990
It did not print "Hey! I'm walkin' here!"

31
00:02:19,000 --> 00:02:24,990
That's because walk in Duck overrides walk in Animal.

32
00:02:25,000 --> 00:02:29,990
The way that works is that Duck inherits Animal and then Duck defines a

33
00:02:30,000 --> 00:02:35,990
method called walk, so it uses this one instead of the one in its parent.

34
00:02:36,000 --> 00:02:40,990
If we wanted to say incorporate the one from its parent, we can do that with the

35
00:02:41,000 --> 00:02:47,990
super function. This is a built-in function which accesses the parent class and

36
00:02:48,000 --> 00:02:53,990
just say, super().walk() like that, and now donald will do both.

37
00:02:54,000 --> 00:02:56,990
Save that and run it, and he says, "Hey! Im walkin here!"

38
00:02:57,000 --> 00:02:58,990
And he says, "Walks like a duck."

39
00:02:59,000 --> 00:03:03,990
So this is how you can access the class in the parent, even though you're

40
00:03:04,000 --> 00:03:06,990
overriding it in the class that inherits from the parent.

41
00:03:07,000 --> 00:03:12,990
So what makes this useful is that now we've defined an animal base class and now

42
00:03:13,000 --> 00:03:15,990
if we want to have another animal, it's very easy for us to do that.

43
00:03:16,000 --> 00:03:23,990
We can say class Dog and it inherits from Animal, and that's really all that we need to define.

44
00:03:24,000 --> 00:03:39,990
We can just say pass here and we can say fido = Dog(), and fido.walk(), and

45
00:03:40,000 --> 00:03:41,990
we have a working dog.

46
00:03:42,000 --> 00:03:43,990
Save that and run it.

47
00:03:44,000 --> 00:03:45,990
And there we have, "Hey! I'm walkin' here!"

48
00:03:46,000 --> 00:03:47,990
And that's from fido.

49
00:03:48,000 --> 00:03:56,990
So obviously a dog, instead of having clothes, he is got fur. So we can say, def

50
00:03:57,000 --> 00:04:04,990
clothes, print, 'I have brown and white fur'.

51
00:04:05,000 --> 00:04:11,990
And then down here if we say fido.clothes(), save that and run it, and it says,

52
00:04:12,000 --> 00:04:13,990
"I have brown and white fur."

53
00:04:14,000 --> 00:04:17,990
But what we've done is we've created a base class that perhaps has all the

54
00:04:18,000 --> 00:04:22,990
properties that are going to be common to the classes that we're creating and

55
00:04:23,000 --> 00:04:27,990
then we simply inherit that and that makes our code very reusable.

56
00:04:28,000 --> 00:04:29,990
You'll see this technique used a lot.

57
00:04:30,000 --> 00:04:34,990
In fact, in an object-oriented language like Python, virtually all of the

58
00:04:35,000 --> 00:04:38,990
classes at some level are inheriting form other classes.

59
00:04:39,000 --> 00:04:43,990
You have two classes like the tuple and the list and they do a lot of

60
00:04:44,000 --> 00:04:45,990
common similar things.

61
00:04:46,000 --> 00:04:47,990
In fact, strings do as well.

62
00:04:48,000 --> 00:04:52,990
And so those are all inheriting from some common classes and simply building

63
00:04:53,000 --> 00:04:56,990
upon that and overwriting some methods and implementing some and not

64
00:04:57,000 --> 00:04:57,990
implementing others.

65
00:04:58,000 --> 00:05:02,990
And that allows them to reuse a lot of code and it also allows them to operate

66
00:05:03,000 --> 00:05:05,990
in a lot of the same contexts as each other.

67
00:05:06,000 --> 00:05:16,000
So this is inheritance and this is how you do inheritance in Python.

