1
00:00:05,260 --> 00:00:09,910
Welcome back, everyone, to part four of our discussion on object oriented programming, or we're talking

2
00:00:09,910 --> 00:00:10,960
about inheritance.

3
00:00:12,270 --> 00:00:17,550
Often Jenga will have classes with many useful methods and attributes already defined for us.

4
00:00:17,850 --> 00:00:22,620
This is kind of the whole point of using Django is that they've already done the hard work of defining

5
00:00:22,890 --> 00:00:26,850
many different Python files, modules, packages and classes within them.

6
00:00:27,330 --> 00:00:33,420
What we would like to do in order to create our own custom sites is create our own derived variations

7
00:00:33,660 --> 00:00:39,060
of those classes that Django provides for us through the use of something known as inheritance.

8
00:00:40,380 --> 00:00:47,100
Inheritance allows us to pass in an existing class into our new custom class, which means we get to

9
00:00:47,100 --> 00:00:53,310
inherit the use of the methods already defined within the parent class, including the in its method.

10
00:00:53,730 --> 00:00:58,350
This saves us a lot of time, and it's going to be a very common syntax in Django that we're going to

11
00:00:58,350 --> 00:01:00,510
discover as we use Django more and more.

12
00:01:01,820 --> 00:01:05,930
So what we're going to do in this case is explore a couple of ideas around inheritance.

13
00:01:06,290 --> 00:01:09,110
First, we're going to discover how to create a parent's class.

14
00:01:09,380 --> 00:01:11,330
This is officially known as a base class.

15
00:01:11,780 --> 00:01:17,510
Then we'll discover how to inherit from that parent class to create a child class officially known as

16
00:01:17,510 --> 00:01:18,740
a derived class.

17
00:01:19,100 --> 00:01:23,990
Then we're going to discuss how to use the inherited methods within the derived class, how to add new

18
00:01:23,990 --> 00:01:26,960
methods, and then how to overwrite derived methods.

19
00:01:27,350 --> 00:01:28,040
Let's get started.

20
00:01:28,310 --> 00:01:28,550
All right.

21
00:01:28,580 --> 00:01:30,290
Here I am inside our code editor.

22
00:01:30,470 --> 00:01:35,660
I'm going to create a very simple class, which is called a person class.

23
00:01:36,140 --> 00:01:42,320
And we're going to say that this person class, when you actually create a new person, the init method

24
00:01:42,500 --> 00:01:46,670
is going to require a first name and last name to create a new person.

25
00:01:47,390 --> 00:01:49,640
So we're going to go ahead and say, here it takes itself.

26
00:01:50,520 --> 00:01:54,630
And then it takes on a first name and it takes in a last name.

27
00:01:55,380 --> 00:01:59,140
And we're going to sign those provided two attributes by the same name.

28
00:01:59,160 --> 00:01:59,610
So.

29
00:02:00,760 --> 00:02:08,080
The first name is equal to the provided first name and then self thought last name is equal to the provided

30
00:02:08,350 --> 00:02:09,039
last name.

31
00:02:09,850 --> 00:02:12,550
Then we're going to do is add in a couple of simple methods.

32
00:02:13,450 --> 00:02:15,340
One method is going to be super simple.

33
00:02:15,430 --> 00:02:17,730
It's just going to say hello.

34
00:02:19,370 --> 00:02:19,880
And self.

35
00:02:21,300 --> 00:02:23,110
And this is just literally going to print out.

36
00:02:23,130 --> 00:02:23,580
Hello.

37
00:02:25,430 --> 00:02:31,580
Then I'll have a slightly more complex method here, which is going to be called Report Peasant Self

38
00:02:31,580 --> 00:02:33,290
here and report.

39
00:02:33,320 --> 00:02:37,160
It's just going to print out this person and who they are.

40
00:02:37,790 --> 00:02:41,090
So using an f string literal here, I'm going to say I am.

41
00:02:41,600 --> 00:02:45,770
And then in curly braces, I can say self that first name.

42
00:02:47,020 --> 00:02:52,870
SpaceX, another set of curly braces to insert a variable here and then self-taught last name.

43
00:02:53,260 --> 00:02:55,430
Don't forget to use self here.

44
00:02:55,690 --> 00:03:00,670
Otherwise, you won't actually be able to connect to the attributes the fine if you just say first name

45
00:03:00,670 --> 00:03:02,620
and last name down here.

46
00:03:02,680 --> 00:03:05,200
It would expect you to pass in first name and last name.

47
00:03:05,210 --> 00:03:05,860
It's a report.

48
00:03:06,490 --> 00:03:08,440
So this is a pretty simple class again.

49
00:03:08,830 --> 00:03:13,930
It's just the person who has a first name and last name, and then this person class has two methods

50
00:03:13,930 --> 00:03:14,650
associated with it.

51
00:03:15,040 --> 00:03:18,100
The ability to print out hello and the ability to report who they are.

52
00:03:18,460 --> 00:03:21,310
Notice both of these are simple print function calls.

53
00:03:21,670 --> 00:03:23,650
Typically, we'd be using something like return.

54
00:03:23,920 --> 00:03:28,060
But for simplicity here, when I'm running these files, I'll just go ahead and print out the results.

55
00:03:29,440 --> 00:03:36,730
So let's create an instance of a person I will say X is equal to an instance of person that requires

56
00:03:36,730 --> 00:03:37,870
a first name and last name.

57
00:03:38,440 --> 00:03:40,150
So let's have that be John Smith.

58
00:03:42,830 --> 00:03:45,620
And then what I'm going to do is say X, that's.

59
00:03:47,520 --> 00:03:48,060
Hello.

60
00:03:49,560 --> 00:03:50,880
Open close parentheses.

61
00:03:51,570 --> 00:03:53,730
And then I'm going to run this code.

62
00:03:55,710 --> 00:03:56,060
Oops!

63
00:03:57,110 --> 00:03:59,750
And I get to see the output here is hello.

64
00:03:59,790 --> 00:04:02,150
And let's try a report to make sure that's also working.

65
00:04:03,770 --> 00:04:05,870
Open close parentheses, because it's a method.

66
00:04:07,460 --> 00:04:07,970
Run that.

67
00:04:08,270 --> 00:04:09,020
Now I see.

68
00:04:09,230 --> 00:04:10,400
I am John Smith.

69
00:04:10,940 --> 00:04:15,770
So clearly, I'm able to, as we've discussed, create an instance of the person class, the find them

70
00:04:15,770 --> 00:04:22,220
of a first name and last name and then use methods either just hello or methods that actually use the

71
00:04:22,220 --> 00:04:27,080
attributes similar to the way we calculated the area and perimeter of a circle.

72
00:04:27,590 --> 00:04:33,920
Now let's imagine I'm going to now create a new class, which is kind of similar to a person, but I

73
00:04:33,920 --> 00:04:38,090
also wanted to have other functionalities through different method calls.

74
00:04:38,570 --> 00:04:44,180
What I can do is I can inherit some of the methods from the person class or really all of them.

75
00:04:44,960 --> 00:04:50,210
So what I'm going to do here is I'm actually going to use Visual Studio Code Editor to collapse this

76
00:04:50,210 --> 00:04:50,930
person class.

77
00:04:51,290 --> 00:04:55,820
Just keep in mind, you can see I've collapsed based off the line numbers here on the left just to save

78
00:04:56,120 --> 00:04:57,260
space on the screen.

79
00:04:57,680 --> 00:05:02,330
Later on, I to show you how to import from other Python files so you don't have to have some huge Python

80
00:05:02,330 --> 00:05:02,690
file.

81
00:05:03,990 --> 00:05:08,550
Now I'm going to create a new class and this is going to be an agent class.

82
00:05:08,820 --> 00:05:12,270
Agents are going to be really similar to a person class.

83
00:05:12,630 --> 00:05:17,730
However, we want them to actually have this method where if I give them the correct passcode, they'll

84
00:05:17,730 --> 00:05:19,800
report back, Hey, I'm a secret agent.

85
00:05:20,400 --> 00:05:24,930
So we're to say class agent and we are going to inherit a person class.

86
00:05:26,370 --> 00:05:28,710
And not how we do that, I simply pass in person.

87
00:05:29,100 --> 00:05:34,650
Also, keep in mind, I'm not passing in person, open close parentheses, just saying person, not

88
00:05:35,160 --> 00:05:35,760
like this.

89
00:05:37,000 --> 00:05:45,250
OK, so we pass in person, and the act of passing in an instance of the person class now means I will

90
00:05:45,250 --> 00:05:53,020
have access to the methods of the person class within agent, which is super useful because now I just

91
00:05:53,020 --> 00:05:56,500
need to worry about the methods that I care about that are specific to agent.

92
00:05:57,100 --> 00:06:05,830
So let's go ahead and create a method here called reveal and reveal takes and self, but also takes

93
00:06:05,830 --> 00:06:07,270
in a passcode.

94
00:06:09,630 --> 00:06:15,420
And we'll say if the passcode is equal to one, two three.

95
00:06:16,680 --> 00:06:18,030
I will go ahead and print.

96
00:06:18,480 --> 00:06:21,720
I am a secret agent.

97
00:06:24,600 --> 00:06:27,090
Else, what can we do here?

98
00:06:27,210 --> 00:06:28,470
I can go ahead and print.

99
00:06:30,540 --> 00:06:31,320
I am.

100
00:06:32,340 --> 00:06:37,740
And then say, self-taught first name and self-taught last name, however, remember I already had a

101
00:06:37,740 --> 00:06:40,230
method really similar to that called report.

102
00:06:40,800 --> 00:06:47,100
So because I inherited person, I can actually now use report anywhere I want.

103
00:06:47,460 --> 00:06:50,260
I could say self-taught report to save myself some time.

104
00:06:50,880 --> 00:06:55,920
So I'm certain instead of this, I'm going to say else self-taught.

105
00:06:57,030 --> 00:06:57,600
Report.

106
00:06:57,870 --> 00:07:03,570
Open close parentheses notice report is not defined anywhere within agents.

107
00:07:04,080 --> 00:07:09,240
What it is, it's it's using the inherited person class to understand that self.

108
00:07:09,240 --> 00:07:14,280
That report means call person that report, which I have up here.

109
00:07:15,540 --> 00:07:18,090
OK, so I'm going to save this.

110
00:07:19,590 --> 00:07:24,840
And let's create first an instance of person just to show you that they're not really being mixed and

111
00:07:24,840 --> 00:07:27,990
matched upwards, they're only going through the inherited class.

112
00:07:28,410 --> 00:07:37,140
So if you create a person, you can say X that report run this python example that pie and forgot to

113
00:07:37,140 --> 00:07:41,280
define last name and first name, let's say again, John Smith here.

114
00:07:43,310 --> 00:07:43,860
Save that.

115
00:07:43,910 --> 00:07:48,320
OK, now let's go ahead and run this, OK, so I see I am John Smith.

116
00:07:48,890 --> 00:07:57,740
Now let's try to call reveal we should expect a problem because I'm trying to call reveal on an instance

117
00:07:57,740 --> 00:07:58,520
of person.

118
00:07:58,790 --> 00:08:03,890
However, reveal is only defined for agent, which does inherits from person.

119
00:08:04,160 --> 00:08:05,600
But this really only goes one way.

120
00:08:05,630 --> 00:08:11,270
This relationship is just agent can now use the stuff with in person, but person has no real association

121
00:08:11,270 --> 00:08:12,020
with agent.

122
00:08:12,560 --> 00:08:17,120
So now if I were to save this, in fact, Visual Studio code tries to warn you by the fact it's not

123
00:08:17,120 --> 00:08:18,440
actually coloring this method.

124
00:08:18,920 --> 00:08:20,420
We should be able to see an error here.

125
00:08:21,260 --> 00:08:26,480
And it says Attribute error person object has no attribute called reveal, essentially warning you,

126
00:08:26,690 --> 00:08:29,150
hey, there's no method or attribute called reveal.

127
00:08:29,600 --> 00:08:34,460
Now let's try this by creating an agent so instead of person here.

128
00:08:35,409 --> 00:08:41,500
I'm going to call agents and an agent has to be constructed the same way a person does, which is to

129
00:08:41,500 --> 00:08:47,710
say it inherits its in its method to construct an agent with a first name and the last name.

130
00:08:48,520 --> 00:08:51,760
So I'm going to create an instance of agents called John Smith.

131
00:08:52,680 --> 00:08:58,950
And let's actually call hello on this agent, know how this actually should work, because the coloring

132
00:08:58,950 --> 00:08:59,610
is different here.

133
00:09:00,390 --> 00:09:06,330
And the most important thing to note here is that hello is not directly defined with an agent.

134
00:09:06,630 --> 00:09:09,930
That's a method agent is inheriting from person.

135
00:09:10,500 --> 00:09:14,170
So if I run this code, I see I get back.

136
00:09:14,220 --> 00:09:14,610
Hello.

137
00:09:15,120 --> 00:09:21,240
I am also able to run report, which is another method the fine of then person.

138
00:09:22,600 --> 00:09:23,380
And I get back.

139
00:09:23,740 --> 00:09:24,640
I am John Smith.

140
00:09:25,300 --> 00:09:34,630
Now what I'm going to do is use the method that is only available for agents, which is reveal, remember,

141
00:09:34,630 --> 00:09:36,610
reveal those require a passcode.

142
00:09:37,030 --> 00:09:40,960
If I just run it like this, I would expect an error saying, Hey, passcode not provided.

143
00:09:41,500 --> 00:09:43,750
So it says Hey, required positional argument passcode.

144
00:09:44,200 --> 00:09:48,670
Let's actually provide the correct passcode, which is reveal passing in one two three there.

145
00:09:49,810 --> 00:09:53,980
I run this ad out, says, Hey, I am a secret agent.

146
00:09:54,610 --> 00:10:01,900
OK, so just to be clear of what's going on here, I have the methods available inside my person class

147
00:10:02,470 --> 00:10:06,100
by passing in person class into agents.

148
00:10:06,550 --> 00:10:14,500
I have no access to all these methods and I can add on additional methods that I only want to be specifically

149
00:10:14,500 --> 00:10:19,450
associated with an instance of this child class, otherwise known as a derived class.

150
00:10:20,110 --> 00:10:26,500
Now, let's imagine I want to actually change what report does within agents.

151
00:10:26,740 --> 00:10:27,460
How do I do that?

152
00:10:27,970 --> 00:10:28,810
It's actually easy.

153
00:10:28,870 --> 00:10:34,810
You can simply overwrite the inherited methods by saying the F and in the name of the method you want

154
00:10:34,810 --> 00:10:37,540
to overwrite, you essentially redefine it like report.

155
00:10:38,110 --> 00:10:42,730
Let's say a secret agent should probably not just report back their name John Smith.

156
00:10:43,180 --> 00:10:48,580
Instead, we're going to say report for an agent is simply saying, I am here.

157
00:10:49,710 --> 00:10:51,960
Instead of saying I am John Smith.

158
00:10:52,650 --> 00:10:58,050
So now when we say reveal and we were passing the wrong passcode, we would expect report to simply

159
00:10:58,050 --> 00:11:00,810
say, I am here for the case of an agent.

160
00:11:01,380 --> 00:11:03,930
So if I run this, I won't get anything different.

161
00:11:04,020 --> 00:11:08,370
He'll still say I'm a secret agent, but let's say the code is now, let's say one two three four.

162
00:11:08,370 --> 00:11:09,300
We i that in wrong.

163
00:11:10,230 --> 00:11:12,900
I run this and it simply says, Now I am here.

164
00:11:13,140 --> 00:11:15,060
It no longer says I am John Smith.

165
00:11:15,240 --> 00:11:24,120
And in fact, if we say x dot and simply call report now, it just says I am here, and that's a way

166
00:11:24,450 --> 00:11:30,320
you can overwrite old methods in your child or derived class.

167
00:11:30,330 --> 00:11:35,220
You just call that method and then overwrite it so you have the ability to use all these methods.

168
00:11:35,640 --> 00:11:40,650
And then if you want it to, you can add new methods and overwrite the existing methods.

169
00:11:41,520 --> 00:11:47,340
The last thing I want to point out here is there may be some instances where you actually want to add

170
00:11:47,340 --> 00:11:50,580
in more attributes beyond first name and last name.

171
00:11:50,940 --> 00:11:56,370
We ourselves won't really see this of jingo, but I do want you to be aware of the syntax for this.

172
00:11:56,760 --> 00:12:02,970
If in fact you do need to add in more attributes than what is provided for us with the parent class.

173
00:12:03,420 --> 00:12:08,430
You would essentially start overwriting the in its method itself, you would say underscore, underscore

174
00:12:09,120 --> 00:12:15,930
in its underscore, underscore and self and then say first name, last name.

175
00:12:15,930 --> 00:12:20,490
And let's imagine for some reason, an agent also needs a code name.

176
00:12:21,480 --> 00:12:29,520
Now I still am inheriting person, so if I want to create an instance a person and then just add on

177
00:12:29,520 --> 00:12:36,900
these attributes, the syntax for this is to say person dot underscore underscore in its underscore,

178
00:12:36,900 --> 00:12:39,300
underscore and then pass in self.

179
00:12:40,280 --> 00:12:47,590
First name, last name, and then we tack on the other attributes that we added here, like code name,

180
00:12:47,600 --> 00:12:52,190
I would then say self code name is equal to code name.

181
00:12:52,550 --> 00:12:56,660
I should point out and be clear here, this isn't really something we do that often within Django,

182
00:12:56,870 --> 00:12:57,920
but it is possible.

183
00:12:57,920 --> 00:13:03,650
And the reason you're doing this is if you have more attributes that you yourself need to add beyond

184
00:13:03,650 --> 00:13:06,560
what's provided for you by the base class.

185
00:13:07,130 --> 00:13:15,590
So then when you define an agent, it would also ask you for a code name like Mr. X or something.

186
00:13:16,540 --> 00:13:19,390
And we can see what report is that actually shouldn't change anything.

187
00:13:19,900 --> 00:13:22,570
So we see still here I am John Smith.

188
00:13:23,020 --> 00:13:26,230
I think we said here report Oh, we actually didn't overwrite anymore.

189
00:13:26,230 --> 00:13:26,860
I deleted that.

190
00:13:27,220 --> 00:13:28,810
Let's actually just ask for its code name.

191
00:13:29,080 --> 00:13:34,210
So I'm going to print out the code name Prince X thought, let's say first name.

192
00:13:35,410 --> 00:13:37,540
And now let's also print out X thought.

193
00:13:39,810 --> 00:13:40,350
Codename.

194
00:13:42,090 --> 00:13:45,810
Save those changes, and now we see, OK, first name.

195
00:13:46,960 --> 00:13:49,900
Is this give ourselves a little more space so we can see the whole thing?

196
00:13:50,380 --> 00:13:52,570
First name is John and then codename was Mr. X.

197
00:13:52,900 --> 00:13:53,200
All right.

198
00:13:53,800 --> 00:14:00,400
So again, as a quick overview, you create the original class, have the init method.

199
00:14:00,820 --> 00:14:04,510
Any other methods you want that can use the functionality of the attributes.

200
00:14:04,930 --> 00:14:09,490
And once you inherit that class again, typically we're not going to be doing something like this,

201
00:14:09,490 --> 00:14:10,240
overwriting it.

202
00:14:10,630 --> 00:14:15,280
So instead, typically what you're going to see is maybe we add some more methods to it and we have

203
00:14:15,280 --> 00:14:19,300
access to those methods within the child class.

204
00:14:19,810 --> 00:14:20,140
All right.

205
00:14:20,170 --> 00:14:21,160
That's it for this lecture.

206
00:14:21,370 --> 00:14:22,210
I'll see you at the next one.

