1
00:00:00,060 --> 00:00:03,780
Let's talk about another feature of Object Oriented

2
00:00:03,780 --> 00:00:06,090
Programming that's really, really handy.

3
00:00:06,510 --> 00:00:11,040
And it's something that we call class inheritance. How does this work?

4
00:00:11,250 --> 00:00:11,660
Well,

5
00:00:11,660 --> 00:00:16,280
let's say that you codet up a robot chef and you give it a bunch of

6
00:00:16,280 --> 00:00:21,170
functionality. You tell it how to bake, how to stir, how to measure.

7
00:00:21,650 --> 00:00:25,190
And then at some point you decide actually, you know, in my restaurant,

8
00:00:25,190 --> 00:00:27,080
I also need a pastry chef.

9
00:00:27,380 --> 00:00:31,940
Now this pastry chef will need to know some of the things that a chef knows how

10
00:00:31,940 --> 00:00:34,760
to do, but it also needs something extra.

11
00:00:35,000 --> 00:00:39,830
So you don't want to create this pastry chef entirely from scratch. Instead,

12
00:00:39,860 --> 00:00:43,580
what you want to do is you want to be able to take the methods you've already

13
00:00:43,580 --> 00:00:47,360
defined for the chef class and then just add to it,

14
00:00:47,570 --> 00:00:50,030
add a few other relevant methods

15
00:00:50,060 --> 00:00:53,270
like how to knead dough or how to whisk some eggs.

16
00:00:53,720 --> 00:00:58,720
So it's process of inheriting behavior and appearance from an existing class is

17
00:01:00,350 --> 00:01:04,760
known as class inheritance. When we're talking about class inheritance,

18
00:01:05,030 --> 00:01:08,600
you can inherit both appearance, so attributes,

19
00:01:08,960 --> 00:01:13,310
like for example, if you inherited the same eyes as your mother,

20
00:01:13,610 --> 00:01:16,310
or if you inherited the same nose as your grandfather,

21
00:01:16,820 --> 00:01:19,100
but you can also inherit behavior.

22
00:01:19,550 --> 00:01:24,550
So maybe you chop tomatoes in the same way that your dad chops tomatoes.

23
00:01:25,220 --> 00:01:30,220
And if you believe my mother, I'm lazy in the same way that my dad is lazy.

24
00:01:30,800 --> 00:01:35,360
So not only can inherit appearance, but we can also inherit behavior.

25
00:01:36,080 --> 00:01:40,340
Now, how does inheritance actually work in terms of the code? Well,

26
00:01:40,340 --> 00:01:43,310
we can define a class, let's call it Fish,

27
00:01:43,760 --> 00:01:46,130
and it has an initializer.

28
00:01:46,610 --> 00:01:51,260
Now in order to get this Fish class to inherit from another class,

29
00:01:51,710 --> 00:01:53,600
all we have to do is to

30
00:01:53,630 --> 00:01:58,630
add a set of parentheses after the name of the class and then provide the

31
00:01:58,730 --> 00:02:00,680
class that we want to inherit from.

32
00:02:01,130 --> 00:02:04,820
So in this case our fish is inheriting from the animal class.

33
00:02:05,360 --> 00:02:10,360
And then in order to get a hold of everything that an animal has and is, so its

34
00:02:11,510 --> 00:02:15,350
attributes and methods, all we have to do is inside the init,

35
00:02:15,500 --> 00:02:17,930
add this super().__init__().

36
00:02:18,410 --> 00:02:21,680
And the super refers to the superclass.

37
00:02:21,890 --> 00:02:26,890
So basically, initialize everything that the superclass can do in our fish

38
00:02:27,530 --> 00:02:31,580
class. Let's take a look at this in terms of real code.

39
00:02:32,180 --> 00:02:32,630
First,

40
00:02:32,630 --> 00:02:37,630
I'm going to create a animal class and this animal class is going to have an

41
00:02:37,940 --> 00:02:42,650
initializer where we're able to define some attributes, right?

42
00:02:42,650 --> 00:02:47,090
So let's say that all animals have, um, two eyes.

43
00:02:47,180 --> 00:02:51,680
So num_eyes = 2. Now at the same time,

44
00:02:51,710 --> 00:02:55,820
I'm also gonna define a method associated with this animal class.

45
00:02:56,150 --> 00:02:58,940
Let's say that all animals know how to breathe.

46
00:02:59,470 --> 00:03:03,430
So inside this breathe method, I'm going to keep it quite simple.

47
00:03:03,430 --> 00:03:05,890
I'm just going to say inhale, exhale.

48
00:03:06,850 --> 00:03:10,210
So that's breathing done. Now later on,

49
00:03:10,210 --> 00:03:15,210
I decide to define a fish class and this fish class probably also knows how to

50
00:03:15,820 --> 00:03:20,500
do certain things. So for example, maybe it knows how to swim, right?

51
00:03:22,900 --> 00:03:26,620
Right now, if I create a object from this Fish class,

52
00:03:26,650 --> 00:03:30,910
let's say I create an object called nemo that's created from the Fish class,

53
00:03:31,240 --> 00:03:33,790
then I can say nemo.swim.

54
00:03:34,840 --> 00:03:39,370
And you can see when I run this code, it says that Nemo is moving in water.

55
00:03:40,030 --> 00:03:40,240
Now,

56
00:03:40,240 --> 00:03:45,240
what if I wanted this Fish class to inherit everything that the animal class can

57
00:03:45,970 --> 00:03:46,600
do,

58
00:03:46,600 --> 00:03:51,600
including the attribute number of eyes and also the method breathe.

59
00:03:52,330 --> 00:03:57,330
All I have to do is to add this animal class after the name of the fish class

60
00:03:58,750 --> 00:03:59,950
inside parentheses,

61
00:04:00,580 --> 00:04:05,580
and then I have to add the initializer and inside the initializer,

62
00:04:06,550 --> 00:04:10,540
I have to trigger a call to the superclass, which is in this case,

63
00:04:10,540 --> 00:04:11,650
the animal class.

64
00:04:12,040 --> 00:04:16,540
And then I'm going to call the initializer, like this.

65
00:04:17,019 --> 00:04:22,019
So now what these two parts of the code does is it allows anything that's

66
00:04:22,390 --> 00:04:27,390
created from my fish class to inherit all of the attributes and methods from the

67
00:04:28,060 --> 00:04:30,160
superclass, which is the animal class.

68
00:04:30,610 --> 00:04:35,610
Now I can get my Nemo to start breathing. And you can see 

69
00:04:35,770 --> 00:04:40,180
if I hit run, then it will also inhale and exhale.

70
00:04:40,780 --> 00:04:41,800
And in addition,

71
00:04:42,010 --> 00:04:45,850
if I wanted to print nemo.num_eyes,

72
00:04:45,940 --> 00:04:48,520
then you can see this is also going to print 2.

73
00:04:49,030 --> 00:04:54,030
So now my object that's created from the Fish class now has access to all the

74
00:04:54,490 --> 00:04:59,490
attributes and methods from the superclass that inherited from the animal class.

75
00:05:00,940 --> 00:05:04,360
Now, what if I wanted to inherit a method?

76
00:05:04,660 --> 00:05:09,130
So get all the things that it does, for example, the breathe method.

77
00:05:09,430 --> 00:05:13,690
But what if I wanted to modify it a little bit? So for example,

78
00:05:13,750 --> 00:05:17,560
a fish does, in fact, breathe, but it kind of breathes underwater.

79
00:05:18,040 --> 00:05:21,520
So let's say I want to define the breathe function

80
00:05:21,790 --> 00:05:26,200
and I wanted to have the same functionality as the superclass

81
00:05:26,200 --> 00:05:30,070
which I'm inheriting from. So I want to print inhale, exhale,

82
00:05:30,340 --> 00:05:33,640
but I also want to do something extra. Well, in this case,

83
00:05:33,670 --> 00:05:38,230
we would get hold of the superclass and then call breathe on it.

84
00:05:39,700 --> 00:05:44,050
This means we're going to do everything that the breathe method from the

85
00:05:44,050 --> 00:05:47,650
superclass does, so all of this, but afterwards

86
00:05:47,680 --> 00:05:51,370
we're going to do something a little bit more special. So we're going to say

87
00:05:51,640 --> 00:05:55,870
we're doing this underwater. Now,

88
00:05:55,900 --> 00:05:59,330
when this line nemo.breathe gets run,

89
00:05:59,630 --> 00:06:03,950
you can see what happens is it will print out inhale, exhale

90
00:06:04,310 --> 00:06:08,540
and this comes from the super class' breathe method here.

91
00:06:09,080 --> 00:06:13,520
And then afterwards, it will print its own unique twist on breathing,

92
00:06:13,550 --> 00:06:18,290
which is doing this underwater. By learning about inheritance,

93
00:06:18,320 --> 00:06:23,030
what it allows us to do is to take an existing class that we've created or

94
00:06:23,030 --> 00:06:24,380
somebody else has created,

95
00:06:24,800 --> 00:06:29,690
and then build on top of it without having to reinvent the wheel and redefine

96
00:06:29,690 --> 00:06:33,200
everything that's in that class. So in the next lesson,

97
00:06:33,230 --> 00:06:37,700
I'm going to show you how we can inherit from the turtle class to upgrade the

98
00:06:37,700 --> 00:06:38,420
turtle

99
00:06:38,420 --> 00:06:43,100
to be able to do extra things such as creating a piece of food or creating a

100
00:06:43,100 --> 00:06:47,120
scoreboard. For all of that and more, I'll see you on the next lesson.

