1
00:00:00,000 --> 00:00:00,000
Hello guys.

2
00:00:00,000 --> 00:00:03,000
So we are going to continue the discussion with respect to Python.

3
00:00:03,000 --> 00:00:06,000
And in this video we are going to talk about encapsulation and abstraction.

4
00:00:06,000 --> 00:00:08,000
Now this will be two videos.

5
00:00:08,000 --> 00:00:13,000
In this specific video we will dedicatedly discuss about encapsulation.

6
00:00:13,000 --> 00:00:18,000
And then in the next video we will talk about abstraction uh encapsulation and abstraction.

7
00:00:18,000 --> 00:00:21,000
They are very important concepts in object oriented programming.

8
00:00:21,000 --> 00:00:27,000
And it will be helpful to design a very good, maintainable and reusable code whenever we define all

9
00:00:27,000 --> 00:00:29,000
these things and use all these things in a project.

10
00:00:29,000 --> 00:00:35,000
Okay, so if I probably go ahead and read this here, you can see an encapsulation involves bundling

11
00:00:35,000 --> 00:00:41,000
data and methods that operate on the data with a single unit, while abstraction involves hiding complex

12
00:00:41,000 --> 00:00:45,000
implementation details and exposing only the necessary feature.

13
00:00:45,000 --> 00:00:51,000
Okay, so abstraction involves hiding complex implementation detail and exposing only the necessary

14
00:00:51,000 --> 00:00:52,000
feature.

15
00:00:52,000 --> 00:00:57,000
So we will be discussing more about this in the next, uh, session because there will be specifically

16
00:00:57,000 --> 00:00:59,000
discussing about abstraction.

17
00:00:59,000 --> 00:01:02,000
But if I talk about encapsulation, it is nothing.

18
00:01:02,000 --> 00:01:07,000
But it is a concept of wrapping data, variables and methods together as a single unit.

19
00:01:07,000 --> 00:01:12,000
It restricts direct access to the same object.

20
00:01:12,000 --> 00:01:18,000
Sorry to some of the object components, which is a mean of preventing accidental inference and misuse

21
00:01:18,000 --> 00:01:19,000
of data.

22
00:01:19,000 --> 00:01:21,000
Now what does this basically mean?

23
00:01:21,000 --> 00:01:24,000
Restrict direct access to some of the object components.

24
00:01:24,000 --> 00:01:29,000
And here we are basically saying hey, it is a concept of wrapping data that is variables and methods

25
00:01:29,000 --> 00:01:31,000
together as a single unit.

26
00:01:31,000 --> 00:01:34,000
So let me just go ahead and show you some of the example over here.

27
00:01:34,000 --> 00:01:38,000
And as I said in this video, we're just going to talk about encapsulation.

28
00:01:38,000 --> 00:01:43,000
And in the next video we'll talk about uh, the next part that is abstraction okay.

29
00:01:43,000 --> 00:01:48,000
So encapsulation we can do with something called as getter and setter method.

30
00:01:48,000 --> 00:01:48,000
Okay.

31
00:01:48,000 --> 00:01:53,000
Now before I go ahead and make you understand about getter and setter method, let me just go ahead

32
00:01:53,000 --> 00:01:57,000
and talk about three important access variables.

33
00:01:57,000 --> 00:01:59,000
One is public okay.

34
00:01:59,000 --> 00:02:05,000
And I hope you may have seen this in other programming language in C sharp in C plus plus.

35
00:02:05,000 --> 00:02:11,000
So public protected and private variables okay.

36
00:02:11,000 --> 00:02:12,000
We'll talk about this.

37
00:02:12,000 --> 00:02:14,000
What exactly it is okay.

38
00:02:14,000 --> 00:02:16,000
Now let's go ahead and do one thing guys.

39
00:02:16,000 --> 00:02:20,000
If I'm going to create a class let's say I'm going to create a class called as person okay.

40
00:02:20,000 --> 00:02:23,000
And let me just go ahead and write definition in it.

41
00:02:23,000 --> 00:02:24,000
Definition.

42
00:02:24,000 --> 00:02:29,000
This is my constructor here I'll take self name and age okay.

43
00:02:29,000 --> 00:02:29,000
Okay.

44
00:02:29,000 --> 00:02:33,000
Now here you can see that if I go ahead and write self dot underscore underscore name.

45
00:02:33,000 --> 00:02:34,000
Okay.

46
00:02:35,000 --> 00:02:38,000
Uh or just let me write self dot name is equal to name okay.

47
00:02:39,000 --> 00:02:43,000
This variable or this instance variable that we are actually creating.

48
00:02:43,000 --> 00:02:47,000
This is basically called as public variables okay.

49
00:02:47,000 --> 00:02:49,000
Public variables okay.

50
00:02:49,000 --> 00:02:52,000
This instance variable or public variable I'll talk about it.

51
00:02:52,000 --> 00:02:53,000
Why we say it as public variable.

52
00:02:53,000 --> 00:02:57,000
And similarly if I go ahead and define this self dot age is equal to age.

53
00:02:57,000 --> 00:02:59,000
This is also a public variable.

54
00:03:00,000 --> 00:03:10,000
This basically means I can I can call this or I can use this particular variable from outside of this

55
00:03:10,000 --> 00:03:10,000
specific class.

56
00:03:10,000 --> 00:03:11,000
Okay.

57
00:03:11,000 --> 00:03:15,000
That basically means okay let's go ahead and see I will go ahead and define person person.

58
00:03:15,000 --> 00:03:22,000
And here I will just say hey the name is crush and here the name is age is 34.

59
00:03:22,000 --> 00:03:22,000
Okay.

60
00:03:23,000 --> 00:03:29,000
Now when I say public variable public access modifier, also I can say see I can also call this as public

61
00:03:29,000 --> 00:03:30,000
access modifiers.

62
00:03:30,000 --> 00:03:34,000
You know we'll also discussing about protected and private access modifiers okay.

63
00:03:34,000 --> 00:03:35,000
We can also talk like that.

64
00:03:35,000 --> 00:03:39,000
Variables or access modifiers okay.

65
00:03:39,000 --> 00:03:42,000
Now here you can see that there is one very important statement.

66
00:03:42,000 --> 00:03:46,000
It restricts a direct access to some of the object components.

67
00:03:46,000 --> 00:03:54,000
Here if I go ahead and print okay person dot name okay I will be able to print it okay.

68
00:03:54,000 --> 00:03:56,000
Here I'm able to get crushed.

69
00:03:56,000 --> 00:04:00,000
Now let's see if I go ahead and use this person okay.

70
00:04:00,000 --> 00:04:09,000
And if I try to understand inside this objects what are the different different inbuilt methods or variables

71
00:04:09,000 --> 00:04:10,000
that are probably available over here.

72
00:04:10,000 --> 00:04:15,000
So if I go ahead and use this Dir function and if I just go ahead and execute it here, you'll be able

73
00:04:15,000 --> 00:04:16,000
to see hey.

74
00:04:16,000 --> 00:04:19,000
And here this is what we use right.

75
00:04:19,000 --> 00:04:21,000
Definition underscore underscore init.

76
00:04:21,000 --> 00:04:21,000
This is a constructor.

77
00:04:21,000 --> 00:04:24,000
So here also you'll be able to find this method is there okay.

78
00:04:25,000 --> 00:04:29,000
And along with this you'll also be seeing there'll be two more variables that are available.

79
00:04:29,000 --> 00:04:31,000
That is age and name.

80
00:04:31,000 --> 00:04:31,000
Right.

81
00:04:31,000 --> 00:04:32,000
And these are nothing.

82
00:04:32,000 --> 00:04:33,000
But these are my public variables.

83
00:04:33,000 --> 00:04:34,000
Right.

84
00:04:34,000 --> 00:04:38,000
Or instance variables, uh, which is specifically over here at public.

85
00:04:38,000 --> 00:04:40,000
What the reason why I'm saying it as public.

86
00:04:40,000 --> 00:04:44,000
Because here you can see that I'm able to see this particular variables directly over here.

87
00:04:44,000 --> 00:04:44,000
Okay.

88
00:04:44,000 --> 00:04:48,000
And I can use it from anywhere of the program just by using the object.

89
00:04:48,000 --> 00:04:52,000
I can use it in different functions anywhere I want specifically.

90
00:04:52,000 --> 00:04:53,000
Okay.

91
00:04:53,000 --> 00:04:59,000
Uh, uh, now, one thing that you can probably do it right, I can see I can probably create a function.

92
00:04:59,000 --> 00:05:04,000
I can, uh, go ahead and write, get underscore name, and I can give the object over here, let's

93
00:05:04,000 --> 00:05:04,000
say person.

94
00:05:04,000 --> 00:05:07,000
And I can go ahead and return something like this.

95
00:05:07,000 --> 00:05:12,000
Return person dot um person dot name.

96
00:05:12,000 --> 00:05:12,000
Okay.

97
00:05:12,000 --> 00:05:15,000
And I will go ahead and execute this okay.

98
00:05:15,000 --> 00:05:17,000
Now let me just go ahead and print this.

99
00:05:17,000 --> 00:05:19,000
So here if I go ahead and write get underscore name.

100
00:05:19,000 --> 00:05:25,000
And if I just call it uh with respect to this particular person here, you can see that I'm able to

101
00:05:25,000 --> 00:05:30,000
access this particular name from outside the function outside the class itself.

102
00:05:30,000 --> 00:05:30,000
Right.

103
00:05:30,000 --> 00:05:37,000
Now, there may be scenario where I may want to define some of the variables in such a way that outside

104
00:05:37,000 --> 00:05:41,000
this class, I should not be able to provide it as an access.

105
00:05:41,000 --> 00:05:42,000
Okay.

106
00:05:42,000 --> 00:05:44,000
Because this may be some very important variables.

107
00:05:44,000 --> 00:05:45,000
Right.

108
00:05:45,000 --> 00:05:49,000
And those variables, I don't want to change it once we are initializing it, we don't really want to

109
00:05:49,000 --> 00:05:51,000
change it in the later stages.

110
00:05:51,000 --> 00:05:51,000
Okay.

111
00:05:51,000 --> 00:05:56,000
If you want to just change it, we should only be able to change it while we are creating the object.

112
00:05:56,000 --> 00:05:56,000
Right?

113
00:05:56,000 --> 00:06:00,000
And I don't want this particular variable to be as an access over there.

114
00:06:00,000 --> 00:06:00,000
Right?

115
00:06:00,000 --> 00:06:05,000
So that is the reason why what we do is that we use something called as protected and private variables.

116
00:06:05,000 --> 00:06:08,000
Now let me just show you one example of creating a private variable.

117
00:06:08,000 --> 00:06:12,000
Right now, if I just go ahead and see directory of person, I'll be able to see all these variables

118
00:06:12,000 --> 00:06:14,000
are publicly available.

119
00:06:14,000 --> 00:06:16,000
Now I will just go ahead and copy the same thing.

120
00:06:16,000 --> 00:06:22,000
Let's see this okay I will I will just go ahead and copy this I'll paste it over here.

121
00:06:22,000 --> 00:06:22,000
Okay.

122
00:06:23,000 --> 00:06:29,000
Um, now instead of just writing underscore underscore innit.

123
00:06:29,000 --> 00:06:31,000
See, I'm creating a person class.

124
00:06:31,000 --> 00:06:35,000
I'm using name and comma age as a parameter while I'm giving it over here.

125
00:06:35,000 --> 00:06:41,000
Now, instead of writing like this, I will use double underscore okay, double underscore name.

126
00:06:41,000 --> 00:06:44,000
And here also I will go ahead and use double underscore age.

127
00:06:44,000 --> 00:06:45,000
Now what is this double underscore name.

128
00:06:45,000 --> 00:06:47,000
This becomes a private variable.

129
00:06:47,000 --> 00:06:51,000
If you want to define a private variable I have to write double underscore.

130
00:06:51,000 --> 00:06:56,000
And here also I will go ahead and write the private variable as double underscore.

131
00:06:56,000 --> 00:06:57,000
Okay.

132
00:06:57,000 --> 00:07:00,000
Now if I go ahead and execute this okay.

133
00:07:00,000 --> 00:07:01,000
Now see see focus over here.

134
00:07:01,000 --> 00:07:03,000
Okay I'll remove all this particular code okay.

135
00:07:04,000 --> 00:07:05,000
And let me do one thing.

136
00:07:05,000 --> 00:07:09,000
Let me just go ahead and execute this particular code okay.

137
00:07:09,000 --> 00:07:11,000
And let me just create an object.

138
00:07:11,000 --> 00:07:12,000
So I will go ahead and create an object.

139
00:07:12,000 --> 00:07:15,000
And here I will write person person.

140
00:07:15,000 --> 00:07:20,000
And let me just go ahead and give my values crush Chris and 34.

141
00:07:20,000 --> 00:07:20,000
Okay.

142
00:07:20,000 --> 00:07:22,000
And let me just make this spelling curse.

143
00:07:23,000 --> 00:07:24,000
Correct.

144
00:07:24,000 --> 00:07:25,000
So it should be person.

145
00:07:25,000 --> 00:07:25,000
Okay.

146
00:07:26,000 --> 00:07:31,000
Now if I go ahead and check d I r with respect to this particular person.

147
00:07:31,000 --> 00:07:36,000
Now see here you can see that I'm not able to see this particular variable right.

148
00:07:36,000 --> 00:07:38,000
Do you see this or let me do one thing.

149
00:07:38,000 --> 00:07:41,000
Let me just create one more public variable along with this.

150
00:07:41,000 --> 00:07:46,000
So here I will go ahead and write self dot um name age.

151
00:07:46,000 --> 00:07:48,000
Let me just go ahead and say that gender okay.

152
00:07:48,000 --> 00:07:53,000
So gender is equal to I will go ahead and assign this particular gender variable which I'm actually

153
00:07:53,000 --> 00:07:54,000
providing over here.

154
00:07:54,000 --> 00:07:54,000
Okay.

155
00:07:55,000 --> 00:07:58,000
Let's say that this information now here you can see this is a public variable.

156
00:07:58,000 --> 00:08:00,000
And this is a private variable.

157
00:08:00,000 --> 00:08:01,000
Why I'm saying it as a private variable.

158
00:08:01,000 --> 00:08:03,000
Because I'll just tell you in some time okay.

159
00:08:03,000 --> 00:08:05,000
Now let's go ahead and update it over here.

160
00:08:05,000 --> 00:08:07,000
And let's say that I'm writing this is mail.

161
00:08:07,000 --> 00:08:08,000
Okay.

162
00:08:08,000 --> 00:08:12,000
If I go ahead and execute it here you can see directory of person.

163
00:08:12,000 --> 00:08:12,000
Okay.

164
00:08:12,000 --> 00:08:16,000
Uh it takes three positional argument but four were given uh three.

165
00:08:16,000 --> 00:08:22,000
One is name age and uh self dot gender is equal to gender.

166
00:08:22,000 --> 00:08:24,000
Let's see why this is an issue over here.

167
00:08:24,000 --> 00:08:28,000
Self dot gender is equal to gender okay I did not execute this.

168
00:08:28,000 --> 00:08:29,000
Maybe okay.

169
00:08:29,000 --> 00:08:34,000
Now it should work right now Here you can actually see that I am able to see this particular public

170
00:08:34,000 --> 00:08:35,000
variable.

171
00:08:35,000 --> 00:08:39,000
Not I'm not able to see all this underscore name on underscore age below this.

172
00:08:39,000 --> 00:08:39,000
Right.

173
00:08:39,000 --> 00:08:40,000
I'm not able to see it.

174
00:08:40,000 --> 00:08:41,000
Why.

175
00:08:41,000 --> 00:08:43,000
Because that is a private variable.

176
00:08:43,000 --> 00:08:47,000
And wherever private variable will be displayed it will be displayed over here in this format.

177
00:08:48,000 --> 00:08:48,000
Right.

178
00:08:48,000 --> 00:08:53,000
So here you can see when I write underscore person underscore underscore age, the private variable

179
00:08:53,000 --> 00:08:56,000
basically becomes in this particular way right.

180
00:08:56,000 --> 00:08:59,000
It will get appended along with the person class.

181
00:08:59,000 --> 00:08:59,000
Right.

182
00:08:59,000 --> 00:09:02,000
So I'm not getting this direct variable.

183
00:09:02,000 --> 00:09:04,000
Any underscore underscore age is no way present.

184
00:09:04,000 --> 00:09:07,000
So this basically indicates that this is a private variable okay.

185
00:09:07,000 --> 00:09:09,000
Now let me do the same thing.

186
00:09:09,000 --> 00:09:11,000
Let me just go ahead and create a function.

187
00:09:11,000 --> 00:09:13,000
And let me say hey get underscore name.

188
00:09:13,000 --> 00:09:19,000
And here I'm actually going to write uh uh I'm going to give an object called as person.

189
00:09:19,000 --> 00:09:28,000
And I'm just going to say return return person person dot underscore underscore name.

190
00:09:28,000 --> 00:09:29,000
Okay.

191
00:09:29,000 --> 00:09:30,000
I'm just going to return this.

192
00:09:30,000 --> 00:09:33,000
Now let me do the same thing over here okay.

193
00:09:33,000 --> 00:09:34,000
Let me just copy this entire thing.

194
00:09:34,000 --> 00:09:40,000
Let me call this person and let me just go ahead and call this particular function name get underscore

195
00:09:40,000 --> 00:09:41,000
name okay.

196
00:09:41,000 --> 00:09:48,000
And if I go ahead and see this and if I just give this particular parameter will I will I will will

197
00:09:48,000 --> 00:09:49,000
the name get printed.

198
00:09:49,000 --> 00:09:51,000
The answer is no.

199
00:09:51,000 --> 00:09:52,000
Why.

200
00:09:52,000 --> 00:09:55,000
Because it is saying that hey it it has no attribute underscore underscore name.

201
00:09:55,000 --> 00:10:01,000
The reason is very simple because this is a private variable and you will not be able to access this.

202
00:10:01,000 --> 00:10:03,000
You will definitely not be able to access it.

203
00:10:03,000 --> 00:10:09,000
Because if I go ahead and see inside the directory I am able to see that, hey my person, uh, the

204
00:10:09,000 --> 00:10:13,000
underscore underscore age has been in this particular name, right?

205
00:10:13,000 --> 00:10:14,000
Yes.

206
00:10:14,000 --> 00:10:15,000
In this directory you're able to see.

207
00:10:15,000 --> 00:10:17,000
But as a developer you are able to see.

208
00:10:17,000 --> 00:10:24,000
But as a common person you will not know that, hey, my private variable has got converted in this

209
00:10:24,000 --> 00:10:25,000
particular name.

210
00:10:25,000 --> 00:10:25,000
Right?

211
00:10:25,000 --> 00:10:28,000
And that is the reason you are not able to access it.

212
00:10:29,000 --> 00:10:32,000
So here we are restricting right?

213
00:10:32,000 --> 00:10:36,000
What happens with the help of access modifiers or encapsulation?

214
00:10:36,000 --> 00:10:42,000
We are restricting some of the variables because I don't want anyone to see this particular variable

215
00:10:42,000 --> 00:10:47,000
or directly access this variable, because these are some very important attributes in a class which

216
00:10:47,000 --> 00:10:49,000
I should not be changing frequently.

217
00:10:49,000 --> 00:10:51,000
Yes, as a developer, I can definitely see it.

218
00:10:51,000 --> 00:10:56,000
I what I can do, I can just go ahead and copy this and I can print it over here.

219
00:10:56,000 --> 00:10:56,000
Okay.

220
00:10:56,000 --> 00:10:58,000
I can do that as a developer.

221
00:10:58,000 --> 00:10:59,000
But this is not a good practice.

222
00:10:59,000 --> 00:11:05,000
The reason we create a private variable, because it is saying to your fellow developers, hey, this

223
00:11:05,000 --> 00:11:08,000
is a private variable and it should always be remaining hidden.

224
00:11:08,000 --> 00:11:11,000
Now if I go ahead and execute like this, I'll be able to get the answer right.

225
00:11:11,000 --> 00:11:14,000
I'll be able to get a 34 right?

226
00:11:14,000 --> 00:11:18,000
Or if I want to probably display the name, I will be able to get this particular name right.

227
00:11:18,000 --> 00:11:24,000
But if I directly call with underscore underscore name, it will not be possible because I'll be getting

228
00:11:24,000 --> 00:11:25,000
that particular error right.

229
00:11:25,000 --> 00:11:28,000
So what does private variable basically say?

230
00:11:28,000 --> 00:11:33,000
That a private variable cannot be accessed from outside the class.

231
00:11:33,000 --> 00:11:34,000
Very simple.

232
00:11:34,000 --> 00:11:36,000
It cannot be accessed from outside the class.

233
00:11:36,000 --> 00:11:38,000
Can we access it inside the class?

234
00:11:38,000 --> 00:11:39,000
Yes, we can do it.

235
00:11:39,000 --> 00:11:42,000
And for that only will be using getter and setter method.

236
00:11:42,000 --> 00:11:43,000
Right.

237
00:11:43,000 --> 00:11:47,000
So private variable is something like which you cannot access outside the class.

238
00:11:47,000 --> 00:11:50,000
Even you have a derived class that can also not access it.

239
00:11:50,000 --> 00:11:51,000
Okay.

240
00:11:51,000 --> 00:11:54,000
So this is one of the very important property in encapsulation.

241
00:11:54,000 --> 00:11:59,000
And this is where we discuss about private and protected public variable.

242
00:11:59,000 --> 00:12:01,000
There is also one more variable which is called as protected.

243
00:12:01,000 --> 00:12:02,000
How do I define protected.

244
00:12:02,000 --> 00:12:04,000
So protected is nothing.

245
00:12:04,000 --> 00:12:06,000
But I will use the same thing.

246
00:12:06,000 --> 00:12:10,000
And let's say instead of writing double underscore, I'll just write single underscore.

247
00:12:10,000 --> 00:12:14,000
So this is what a protected variable is like, right?

248
00:12:14,000 --> 00:12:20,000
A protected variable or access modifier is like okay, a protected variable basically says that, hey,

249
00:12:20,000 --> 00:12:23,000
you cannot use it outside the class.

250
00:12:24,000 --> 00:12:30,000
Like outside the class basically means we cannot access this underscore name outside the class, okay.

251
00:12:30,000 --> 00:12:32,000
But we can access it from a derived class.

252
00:12:32,000 --> 00:12:34,000
So let me just go ahead and write.

253
00:12:34,000 --> 00:12:38,000
Let's say if I go ahead and say employee is my another class okay.

254
00:12:38,000 --> 00:12:40,000
Employee is my another class.

255
00:12:40,000 --> 00:12:42,000
And if I say hey this is inheriting person.

256
00:12:42,000 --> 00:12:46,000
So here I have created a class employee which is inheriting person.

257
00:12:46,000 --> 00:12:49,000
Now what I'm actually going to do, I'll just go ahead and write my init function.

258
00:12:49,000 --> 00:12:50,000
Right.

259
00:12:50,000 --> 00:12:53,000
So here I'm again going to write my init function.

260
00:12:53,000 --> 00:12:57,000
And inside this particular init function I'm just going to pass all the parameters like let's say age,

261
00:12:57,000 --> 00:12:58,000
gender.

262
00:12:58,000 --> 00:12:59,000
Okay.

263
00:12:59,000 --> 00:13:02,000
And what I'm actually going to do over here, I'll go inside this.

264
00:13:02,000 --> 00:13:05,000
And now I'm going to just write super Dot.

265
00:13:05,000 --> 00:13:08,000
And since I'm inheriting all the attributes from the upper class.

266
00:13:08,000 --> 00:13:15,000
So I will just go ahead and write this self comma name comma h comma gender okay.

267
00:13:15,000 --> 00:13:21,000
Now here you'll be able to see that, uh, if I have this class employee right Right, I should be able

268
00:13:21,000 --> 00:13:23,000
to access this particular values.

269
00:13:23,000 --> 00:13:24,000
Okay.

270
00:13:24,000 --> 00:13:27,000
Uh, and that is what protected variable is all about, right?

271
00:13:27,000 --> 00:13:31,000
Whenever we create a protected variable in one of the classes, only its derived class will be able

272
00:13:31,000 --> 00:13:32,000
to access it.

273
00:13:32,000 --> 00:13:36,000
Other than that, you won't be able to access it outside that particular class.

274
00:13:36,000 --> 00:13:41,000
Okay, so once I probably create this derived class now from this derived class, I should be able to

275
00:13:41,000 --> 00:13:42,000
access all these variables.

276
00:13:42,000 --> 00:13:45,000
So what I will do, I will just remove all these things Okay.

277
00:13:45,000 --> 00:13:47,000
I will not be requiring over here.

278
00:13:47,000 --> 00:13:50,000
Now let me just go ahead and create my employee.

279
00:13:50,000 --> 00:13:50,000
Okay.

280
00:13:50,000 --> 00:13:53,000
And here I'm going to define my employee okay.

281
00:13:53,000 --> 00:13:58,000
And let's say that I'm going to give some values like crush or 34.

282
00:13:58,000 --> 00:14:02,000
And here I'm going to give the gender as male okay.

283
00:14:02,000 --> 00:14:09,000
Now I will just go ahead and print employee dot underscore name.

284
00:14:09,000 --> 00:14:10,000
Right.

285
00:14:10,000 --> 00:14:14,000
So once I execute it here okay five parameters is required.

286
00:14:14,000 --> 00:14:17,000
So let's see which all parameters name age gender.

287
00:14:17,000 --> 00:14:21,000
Uh over here I'm given name age gender.

288
00:14:21,000 --> 00:14:26,000
And here you will be able to see that uh underscore name underscore age.

289
00:14:26,000 --> 00:14:30,000
Um, it takes four positional arguments but five were given.

290
00:14:30,000 --> 00:14:31,000
Let's see where it is.

291
00:14:31,000 --> 00:14:32,000
Four.

292
00:14:32,000 --> 00:14:34,000
Five were given.

293
00:14:34,000 --> 00:14:35,000
Uh, so self is over here.

294
00:14:36,000 --> 00:14:36,000
Employee.

295
00:14:36,000 --> 00:14:38,000
Person is over here.

296
00:14:38,000 --> 00:14:39,000
Employee is over here.

297
00:14:39,000 --> 00:14:43,000
And this is nothing but employee dot underscore name.

298
00:14:43,000 --> 00:14:46,000
Okay guys one mistake that I have done over here I've used self keyword.

299
00:14:46,000 --> 00:14:48,000
So I should not be using this now.

300
00:14:48,000 --> 00:14:50,000
I think it should be displaying it okay.

301
00:14:50,000 --> 00:14:56,000
So employee dot underscore name here you'll be able to see that I'll be able to get this particular

302
00:14:56,000 --> 00:14:57,000
output.

303
00:14:57,000 --> 00:15:04,000
So in the case of whenever we define a protected variable it is it it is just like so it is not anything

304
00:15:04,000 --> 00:15:05,000
is compulsory in Python.

305
00:15:05,000 --> 00:15:11,000
You know, it is uh, like a very uh, it is a kind of programming language where there are no strict

306
00:15:11,000 --> 00:15:14,000
rules, but most of the concepts that we learn.

307
00:15:14,000 --> 00:15:14,000
Right.

308
00:15:14,000 --> 00:15:18,000
Let's say that if I'm trying to create a private variable, if I'm trying to create a protected variable

309
00:15:18,000 --> 00:15:23,000
in case of protected variable, I have to always make sure that this variable will only be accessible

310
00:15:23,000 --> 00:15:24,000
only from the derived class.

311
00:15:24,000 --> 00:15:29,000
In the case of private variable, nothing should be accessible outside the class, you know, and public

312
00:15:29,000 --> 00:15:33,000
variable is what you should be able to see that you will be able to access it from anywhere, right?

313
00:15:33,000 --> 00:15:38,000
So here I have actually shown you the example of all these things, uh, with respect to private variable,

314
00:15:38,000 --> 00:15:40,000
with respect to public variable and all.

315
00:15:40,000 --> 00:15:46,000
And uh, let's say that if I am creating a private variable, if I'm not able to access it from outside

316
00:15:46,000 --> 00:15:50,000
the class, can we access it from methods that are present inside the class?

317
00:15:50,000 --> 00:15:50,000
Okay.

318
00:15:50,000 --> 00:15:53,000
And that is what, uh, we are basically going to do now.

319
00:15:53,000 --> 00:15:54,000
Okay.

320
00:15:54,000 --> 00:15:58,000
And, uh, we are going to learn encapsulation with the help of getter and setter method.

321
00:15:58,000 --> 00:16:04,000
So let's go ahead and write encapsulation with getter and setter okay.

322
00:16:05,000 --> 00:16:09,000
Now what I am actually going to do over here is that, uh, first of all I'm just going to create my

323
00:16:09,000 --> 00:16:10,000
class.

324
00:16:10,000 --> 00:16:15,000
So let me just go ahead and write class person and this will be definition init underscore underscore.

325
00:16:15,000 --> 00:16:19,000
And here I'm going to basically write self name age okay.

326
00:16:20,000 --> 00:16:23,000
And here you can see self dot underscore underscore name.

327
00:16:23,000 --> 00:16:26,000
So this will basically be my private variable I will go ahead and initialize it.

328
00:16:26,000 --> 00:16:31,000
Private access modifier or variable okay.

329
00:16:31,000 --> 00:16:33,000
Or variable or variable.

330
00:16:34,000 --> 00:16:35,000
Okay.

331
00:16:35,000 --> 00:16:37,000
And uh, see it is very simple.

332
00:16:37,000 --> 00:16:40,000
I need to probably not display this anywhere.

333
00:16:40,000 --> 00:16:42,000
It should be restricted within the class itself.

334
00:16:42,000 --> 00:16:43,000
That is what is my aim.

335
00:16:44,000 --> 00:16:48,000
Right then I have self underscore age which will be equal to age itself.

336
00:16:48,000 --> 00:16:52,000
And here I'm going to probably say that this is also my private variable.

337
00:16:52,000 --> 00:16:53,000
Okay.

338
00:16:53,000 --> 00:16:58,000
Now in order to access this particular variable inside the class I can definitely do it.

339
00:16:58,000 --> 00:17:03,000
And for this we'll be using something called as getter method for name.

340
00:17:03,000 --> 00:17:03,000
Okay.

341
00:17:03,000 --> 00:17:07,000
So we'll go ahead and create one function which is called as get underscore name.

342
00:17:07,000 --> 00:17:09,000
And here we're going to use the self keyword.

343
00:17:09,000 --> 00:17:14,000
And with the help of this self keyword wherever we use we should be able to access the underscore underscore

344
00:17:14,000 --> 00:17:15,000
name.

345
00:17:15,000 --> 00:17:19,000
So here I'm going to return self dot underscore underscore name okay.

346
00:17:19,000 --> 00:17:23,000
So this is basically my getter method for name right.

347
00:17:23,000 --> 00:17:28,000
So here you can see that I will just be exposing this particular method to the outside world.

348
00:17:28,000 --> 00:17:30,000
Because this is a public method right.

349
00:17:30,000 --> 00:17:32,000
I will not be exposing underscore underscore name.

350
00:17:32,000 --> 00:17:36,000
With the help of this particular method I should be able to get this right.

351
00:17:36,000 --> 00:17:39,000
Now here is what I will talk about.

352
00:17:39,000 --> 00:17:40,000
One very important thing right.

353
00:17:40,000 --> 00:17:42,000
Have you seen a washing machine.

354
00:17:42,000 --> 00:17:47,000
So if I am probably working on the washing machine in washing machine will be able to see buttons,

355
00:17:47,000 --> 00:17:51,000
will be able to see how to start, will be able to see that where we need to put the clothes right.

356
00:17:51,000 --> 00:17:55,000
But internal working is completely hidden, right?

357
00:17:55,000 --> 00:17:56,000
And that is what over here.

358
00:17:56,000 --> 00:17:58,000
Same thing we are doing.

359
00:17:58,000 --> 00:18:00,000
The internal things are probably hidden over here.

360
00:18:00,000 --> 00:18:02,000
Only the things that matter to us.

361
00:18:02,000 --> 00:18:06,000
You know, just like the function name is available for us or visible to us.

362
00:18:06,000 --> 00:18:07,000
Right?

363
00:18:07,000 --> 00:18:11,000
So here you can see that just by calling this particular method get underscore name, I should be able

364
00:18:11,000 --> 00:18:15,000
to get the uh name itself uh, from this particular function.

365
00:18:15,000 --> 00:18:17,000
This is called as a getter method.

366
00:18:17,000 --> 00:18:21,000
I may also have setter method setter method for name.

367
00:18:21,000 --> 00:18:23,000
Right now this setter method for name.

368
00:18:23,000 --> 00:18:30,000
What it does is that this will help us to change any values that I give to the name.

369
00:18:30,000 --> 00:18:34,000
The name, which is basically a private variable right over here.

370
00:18:34,000 --> 00:18:35,000
So this is my private variable.

371
00:18:35,000 --> 00:18:39,000
I need to probably change that particular value, let's say.

372
00:18:39,000 --> 00:18:41,000
So I can just use self underscore.

373
00:18:41,000 --> 00:18:43,000
Underscore name is equal to name right.

374
00:18:43,000 --> 00:18:45,000
So with the help of getter we are retrieving the data.

375
00:18:45,000 --> 00:18:49,000
With the help of setter we are changing the name itself okay.

376
00:18:49,000 --> 00:18:55,000
Similarly let's say that uh uh I, I want it for the age.

377
00:18:55,000 --> 00:19:00,000
And uh, uh, I can also do that, uh, you know, and probably use a getter method for that and setter

378
00:19:00,000 --> 00:19:01,000
method for that.

379
00:19:01,000 --> 00:19:04,000
So first I will do create a getter method okay.

380
00:19:04,000 --> 00:19:09,000
And here you can see I'm getting the age just writing self dot underscore age.

381
00:19:09,000 --> 00:19:15,000
And now in the case of setter method uh let's say that I want to probably put or change any age.

382
00:19:15,000 --> 00:19:15,000
Right.

383
00:19:15,000 --> 00:19:17,000
So what I do I first of all check the condition.

384
00:19:17,000 --> 00:19:20,000
If age is greater than zero, then only I will go ahead and change it.

385
00:19:20,000 --> 00:19:23,000
Otherwise I'll say age cannot be negative, right?

386
00:19:23,000 --> 00:19:26,000
So this is just one example with encapsulation with uh, getter and setter.

387
00:19:26,000 --> 00:19:30,000
And we are completely hiding the main variables that we have.

388
00:19:30,000 --> 00:19:30,000
Right.

389
00:19:30,000 --> 00:19:32,000
And we are only exposing this particular functions.

390
00:19:32,000 --> 00:19:34,000
That is get underscore name and setter underscore name.

391
00:19:35,000 --> 00:19:36,000
Now let me do one thing.

392
00:19:36,000 --> 00:19:39,000
Let me just go ahead and call all this functions okay.

393
00:19:39,000 --> 00:19:41,000
And then you'll be able to understand.

394
00:19:41,000 --> 00:19:43,000
First of all I'll go ahead and create an object.

395
00:19:43,000 --> 00:19:44,000
Let's say this will be person.

396
00:19:44,000 --> 00:19:47,000
And here I'm going to use crush.

397
00:19:47,000 --> 00:19:49,000
Here I'm going to use 34 okay.

398
00:19:50,000 --> 00:20:04,000
Now you will be able to see here what I'm actually going to do is that access and modify private variables

399
00:20:04,000 --> 00:20:08,000
using getter and setter okay.

400
00:20:08,000 --> 00:20:13,000
This is what we are going to do because obviously we cannot use directly the private variable outside

401
00:20:13,000 --> 00:20:13,000
the class.

402
00:20:13,000 --> 00:20:14,000
Right.

403
00:20:14,000 --> 00:20:19,000
So here I'm going to just print person dot get underscore name.

404
00:20:19,000 --> 00:20:23,000
So if I want to go ahead and display get underscore name I should be able to display it.

405
00:20:23,000 --> 00:20:29,000
Then if I go ahead and print person dot get underscore age.

406
00:20:29,000 --> 00:20:31,000
So I should be able to get that also.

407
00:20:32,000 --> 00:20:37,000
And then I will just go ahead and write person dot set underscore h.

408
00:20:37,000 --> 00:20:40,000
And here I'm going to basically write 35.

409
00:20:40,000 --> 00:20:40,000
Right.

410
00:20:40,000 --> 00:20:47,000
And then I'm going to print uh person dot get age again get underscore age.

411
00:20:47,000 --> 00:20:50,000
So I should be able to see the age now okay.

412
00:20:50,000 --> 00:20:51,000
Yeah.

413
00:20:51,000 --> 00:20:57,000
And then I will go ahead and set the age let's say to minus five so that I get the other error that

414
00:20:57,000 --> 00:20:58,000
I have actually written.

415
00:20:58,000 --> 00:21:04,000
Now, initially I'm creating an object, then I'm probably displaying the name displaying the age.

416
00:21:04,000 --> 00:21:06,000
So crush and 34 will be the answer.

417
00:21:06,000 --> 00:21:09,000
Then when we set the age then it will get replaced with 35.

418
00:21:09,000 --> 00:21:11,000
And then we can actually see this right?

419
00:21:11,000 --> 00:21:12,000
So crush 34.

420
00:21:12,000 --> 00:21:17,000
Then we uh, you can see over here is that, uh, what we are doing?

421
00:21:17,000 --> 00:21:21,000
We have we are printing uh, you are setting this age and then you are printing that particular age.

422
00:21:21,000 --> 00:21:26,000
And then when you are setting it to minus five, you can see that it is showing age cannot be negative,

423
00:21:26,000 --> 00:21:27,000
right.

424
00:21:27,000 --> 00:21:31,000
So this is, uh, what is all about encapsulation.

425
00:21:31,000 --> 00:21:36,000
We have discussed about all the access modifiers like public, private and public, uh, public, private

426
00:21:36,000 --> 00:21:37,000
protected.

427
00:21:37,000 --> 00:21:41,000
You understood that, uh, private variables cannot be accessed outside the class, you know?

428
00:21:41,000 --> 00:21:43,000
So that is the reason we create getter and setter method.

429
00:21:43,000 --> 00:21:46,000
Public variables can be accessed much easily protected.

430
00:21:46,000 --> 00:21:49,000
Needs to be accessed only from the derived class.

431
00:21:49,000 --> 00:21:51,000
But again, in Python nothing is compulsory.

432
00:21:51,000 --> 00:21:55,000
As I showed you when I checked the dir directory of that particular object.

433
00:21:55,000 --> 00:21:56,000
Right?

434
00:21:56,000 --> 00:21:59,000
I should be able to see all the private and public variables, right?

435
00:21:59,000 --> 00:22:02,000
And with just with the help of those name, I can directly call it.

436
00:22:02,000 --> 00:22:06,000
But it is always a good practice that we should follow whenever we are implementing things.

437
00:22:06,000 --> 00:22:07,000
So yes, this was it.

438
00:22:07,000 --> 00:22:10,000
Uh, in the next video we are going to discuss about abstraction.

439
00:22:10,000 --> 00:22:11,000
Thank you.

440
00:22:11,000 --> 00:22:11,000
Take care.

441
00:22:11,000 --> 00:22:12,000
Bye bye.

