WEBVTT

0
00:00.050 --> 00:06.260
Let's talk about another feature of Object-Oriented Programming that's really, really handy.

1
00:06.260 --> 00:09.590
And it's something that we call Class Inheritance.

2
00:09.740 --> 00:11.090
How does this work?

3
00:11.090 --> 00:17.750
Well, let's say that you code up a Robot Chef and you give it a bunch of functionality like you tell

4
00:17.750 --> 00:21.320
it how to bake, how to stir, how to measure,

5
00:21.320 --> 00:27.170
and then at some point, you decide actually, you know, in my restaurant, I also need a Pastry Chef.

6
00:27.170 --> 00:33.280
Now, this pastry chef will need to know some of the things that a chef knows how to do, but it also

7
00:33.280 --> 00:34.810
needs something extra.

8
00:34.810 --> 00:38.950
So you don't want to create this pastry chef entirely from scratch.

9
00:38.950 --> 00:44.740
Instead, what you want to do is you want to be able to take the methods you already defined for the

10
00:44.740 --> 00:47.470
Chef class and then just add to it.

11
00:47.470 --> 00:53.470
Add a few other relevant methods like how to knead dough, or how to whisk some eggs.

12
00:53.470 --> 01:02.500
So this process of inheriting behavior and appearance from an existing class is known as Class Inheritance.

13
01:02.620 --> 01:07.360
When we're talking about class inheritance, you can inherit both appearance,

14
01:07.360 --> 01:14.380
so attributes, like for example, if you inherited the same eyes as your mother, or if you inherited

15
01:14.380 --> 01:19.300
the same nose as your grandfather, but you can also inherit behavior.

16
01:19.300 --> 01:24.970
So maybe you chop tomatoes in the same way that your dad chops tomatoes.

17
01:24.970 --> 01:30.550
And if you believe my mother, I'm lazy in the same way that my dad is lazy.

18
01:30.550 --> 01:35.710
So not only can win inherit appearance, but we can also inherit behavior.

19
01:35.740 --> 01:39.850
Now, how does inheritance actually work in terms of the code?

20
01:39.880 --> 01:43.450
Well, we can define a class, let's call it Fish,

21
01:43.450 --> 01:46.280
and it has an initializer.

22
01:46.310 --> 01:54.350
Now, in order to get this Fish class to inherit from another class, all we have to do is to add a

23
01:54.350 --> 02:00.200
set of parentheses after the name of the class, and then provide the class that we want to inherit

24
02:00.200 --> 02:00.800
from.

25
02:00.830 --> 02:05.030
So in this case, our Fish is inheriting from the Animal class.

26
02:05.030 --> 02:12.890
And then in order to get hold of everything that an animal has and is so its attributes and methods,

27
02:12.890 --> 02:15.320
all we have to do is inside the __init__(),

28
02:15.320 --> 02:21.680
add this super().__init__(), and this super refers to the superclass.

29
02:21.680 --> 02:28.250
So basically initialize everything that the superclass can do in our Fish class.

30
02:28.250 --> 02:31.820
Let's take a look at this in terms of real code.

31
02:31.850 --> 02:34.970
First I'm going to create an Animal class,

32
02:34.970 --> 02:42.310
and this Animal class is going to have an initializer where we're able to define some attributes.

33
02:42.310 --> 02:42.580
Right?

34
02:42.580 --> 02:47.050
So let's say that all animals have two eyes,

35
02:47.050 --> 02:49.990
so num_eyes = 2.

36
02:50.080 --> 02:55.900
Now at the same time, I'm also going to define a method associated with this Animal class.

37
02:55.900 --> 02:59.170
Let's say that all animals know how to breathe.

38
02:59.170 --> 03:03.390
So inside this breathe() method, I'm going to keep it quite simple,

39
03:03.390 --> 03:05.970
I'm just going to say, ("Inhale, exhale").

40
03:06.690 --> 03:08.790
So that's breathing done.

41
03:08.790 --> 03:13.050
Now later on I decide to define a Fish class,

42
03:13.140 --> 03:16.860
and this Fish class probably also knows how to do certain things.

43
03:16.860 --> 03:20.670
So for example maybe it knows how to swim, right?

44
03:22.740 --> 03:23.190
Right

45
03:23.190 --> 03:29.690
now if I create an object from this Fish class, let's say I create an object called nemo that's created

46
03:29.690 --> 03:30.980
from the Fish class,

47
03:30.980 --> 03:34.040
then I can say nemo.swim().

48
03:34.520 --> 03:39.620
And you can see when I run this code it says that nemo is moving in water.

49
03:39.740 --> 03:47.390
Now what if I wanted this Fish class to inherit everything that the Animal class can do, including

50
03:47.390 --> 03:51.980
the attribute, num_eyes, and also the method breathe()?

51
03:52.010 --> 04:00.650
All I have to do is to add this Animal class after the name of the Fish class inside parentheses, and

52
04:00.650 --> 04:09.230
then I have to add the initializer and inside the initializer, I have to trigger a call to the superclass(),

53
04:09.230 --> 04:11.780
which is in this case the Animal class,

54
04:11.780 --> 04:16.720
and then I'm going to call the initializer like this.

55
04:16.720 --> 04:23.860
So now what these two parts of the code does is it allows anything that's created from my Fish class

56
04:23.860 --> 04:30.340
to inherit all of the attributes and methods from the superclass, which is the Animal class.

57
04:30.340 --> 04:39.580
Now I can get my nemo to start breathing and you can see if I hit Run, then it will also Inhale and

58
04:39.580 --> 04:40.450
exhale.

59
04:40.450 --> 04:48.030
And in addition, if I wanted to print, nemo.num_eyes then you can see this is also going to print

60
04:48.030 --> 04:48.690
too.

61
04:48.720 --> 04:56.550
So now my object that's created from the Fish class now has access to all the attributes and methods

62
04:56.550 --> 05:00.570
from the superclass that inherited from the Animal class.

63
05:00.600 --> 05:08.580
Now what if I wanted to inherit a method so get all the things that it does, for example in the breathe()

64
05:08.580 --> 05:09.180
method,

65
05:09.180 --> 05:12.390
but what if I wanted to modify it a little bit?

66
05:12.390 --> 05:17.760
So for example, a Fish does in fact breathe, but it kind of breathes underwater.

67
05:17.760 --> 05:21.570
So let's say I wanted to define the breathe() function,

68
05:21.570 --> 05:27.420
and I want it to have the same functionality as the superclass which I'm inheriting from.

69
05:27.420 --> 05:30.090
So I want to print, inhale, exhale,

70
05:30.090 --> 05:32.540
but I also want to do something extra.

71
05:32.570 --> 05:38.390
Well in this case we would get hold of the superclass and then call breathe() on it.

72
05:39.470 --> 05:45.020
This means we're going to do everything that the breathe() method from the superclass does.

73
05:45.020 --> 05:46.280
So all of this.

74
05:46.280 --> 05:50.150
But afterwards we're going to do something a little bit more special.

75
05:50.150 --> 05:54.980
So we're going to say, "We're doing this underwater."

76
05:55.010 --> 06:03.310
Now when this line nemo. breathe() gets run, you can see what happens is it will print out Inhale,

77
06:03.310 --> 06:04.090
exhale,

78
06:04.090 --> 06:08.740
and this comes from the superclass's breathe() method here.

79
06:08.740 --> 06:16.090
And then afterwards it will print its own unique twist on breathing, which is, "doing this underwater."

80
06:16.420 --> 06:18.250
By learning about inheritance,

81
06:18.250 --> 06:24.520
what it allows us to do is to take an existing class that we've created, or somebody else has created,

82
06:24.520 --> 06:30.940
and then build on top of it without having to reinvent the wheel and redefine everything that's in that

83
06:30.940 --> 06:31.690
class.

84
06:31.690 --> 06:37.540
So in the next lesson, I'm going to show you how we can inherit from the turtle class, to upgrade

85
06:37.540 --> 06:44.020
the turtle, to be able to do extra things, such as creating a piece of food, or creating a scoreboard

86
06:44.020 --> 06:45.760
For all of that and more,

87
06:45.760 --> 06:47.380
I'll see you on the next lesson.