Hello guys.
So we are going to continue the discussion with respect to Python.
And in this video we are going to talk about encapsulation and abstraction.
Now this will be two videos.
In this specific video we will dedicatedly discuss about encapsulation.
And then in the next video we will talk about abstraction uh encapsulation and abstraction.
They are very important concepts in object oriented programming.
And it will be helpful to design a very good, maintainable and reusable code whenever we define all
these things and use all these things in a project.
Okay, so if I probably go ahead and read this here, you can see an encapsulation involves bundling
data and methods that operate on the data with a single unit, while abstraction involves hiding complex
implementation details and exposing only the necessary feature.
Okay, so abstraction involves hiding complex implementation detail and exposing only the necessary
feature.
So we will be discussing more about this in the next, uh, session because there will be specifically
discussing about abstraction.
But if I talk about encapsulation, it is nothing.
But it is a concept of wrapping data, variables and methods together as a single unit.
It restricts direct access to the same object.
Sorry to some of the object components, which is a mean of preventing accidental inference and misuse
of data.
Now what does this basically mean?
Restrict direct access to some of the object components.
And here we are basically saying hey, it is a concept of wrapping data that is variables and methods
together as a single unit.
So let me just go ahead and show you some of the example over here.
And as I said in this video, we're just going to talk about encapsulation.
And in the next video we'll talk about uh, the next part that is abstraction okay.
So encapsulation we can do with something called as getter and setter method.
Okay.
Now before I go ahead and make you understand about getter and setter method, let me just go ahead
and talk about three important access variables.
One is public okay.
And I hope you may have seen this in other programming language in C sharp in C plus plus.
So public protected and private variables okay.
We'll talk about this.
What exactly it is okay.
Now let's go ahead and do one thing guys.
If I'm going to create a class let's say I'm going to create a class called as person okay.
And let me just go ahead and write definition in it.
Definition.
This is my constructor here I'll take self name and age okay.
Okay.
Now here you can see that if I go ahead and write self dot underscore underscore name.
Okay.
Uh or just let me write self dot name is equal to name okay.
This variable or this instance variable that we are actually creating.
This is basically called as public variables okay.
Public variables okay.
This instance variable or public variable I'll talk about it.
Why we say it as public variable.
And similarly if I go ahead and define this self dot age is equal to age.
This is also a public variable.
This basically means I can I can call this or I can use this particular variable from outside of this
specific class.
Okay.
That basically means okay let's go ahead and see I will go ahead and define person person.
And here I will just say hey the name is crush and here the name is age is 34.
Okay.
Now when I say public variable public access modifier, also I can say see I can also call this as public
access modifiers.
You know we'll also discussing about protected and private access modifiers okay.
We can also talk like that.
Variables or access modifiers okay.
Now here you can see that there is one very important statement.
It restricts a direct access to some of the object components.
Here if I go ahead and print okay person dot name okay I will be able to print it okay.
Here I'm able to get crushed.
Now let's see if I go ahead and use this person okay.
And if I try to understand inside this objects what are the different different inbuilt methods or variables
that are probably available over here.
So if I go ahead and use this Dir function and if I just go ahead and execute it here, you'll be able
to see hey.
And here this is what we use right.
Definition underscore underscore init.
This is a constructor.
So here also you'll be able to find this method is there okay.
And along with this you'll also be seeing there'll be two more variables that are available.
That is age and name.
Right.
And these are nothing.
But these are my public variables.
Right.
Or instance variables, uh, which is specifically over here at public.
What the reason why I'm saying it as public.
Because here you can see that I'm able to see this particular variables directly over here.
Okay.
And I can use it from anywhere of the program just by using the object.
I can use it in different functions anywhere I want specifically.
Okay.
Uh, uh, now, one thing that you can probably do it right, I can see I can probably create a function.
I can, uh, go ahead and write, get underscore name, and I can give the object over here, let's
say person.
And I can go ahead and return something like this.
Return person dot um person dot name.
Okay.
And I will go ahead and execute this okay.
Now let me just go ahead and print this.
So here if I go ahead and write get underscore name.
And if I just call it uh with respect to this particular person here, you can see that I'm able to
access this particular name from outside the function outside the class itself.
Right.
Now, there may be scenario where I may want to define some of the variables in such a way that outside
this class, I should not be able to provide it as an access.
Okay.
Because this may be some very important variables.
Right.
And those variables, I don't want to change it once we are initializing it, we don't really want to
change it in the later stages.
Okay.
If you want to just change it, we should only be able to change it while we are creating the object.
Right?
And I don't want this particular variable to be as an access over there.
Right?
So that is the reason why what we do is that we use something called as protected and private variables.
Now let me just show you one example of creating a private variable.
Right now, if I just go ahead and see directory of person, I'll be able to see all these variables
are publicly available.
Now I will just go ahead and copy the same thing.
Let's see this okay I will I will just go ahead and copy this I'll paste it over here.
Okay.
Um, now instead of just writing underscore underscore innit.
See, I'm creating a person class.
I'm using name and comma age as a parameter while I'm giving it over here.
Now, instead of writing like this, I will use double underscore okay, double underscore name.
And here also I will go ahead and use double underscore age.
Now what is this double underscore name.
This becomes a private variable.
If you want to define a private variable I have to write double underscore.
And here also I will go ahead and write the private variable as double underscore.
Okay.
Now if I go ahead and execute this okay.
Now see see focus over here.
Okay I'll remove all this particular code okay.
And let me do one thing.
Let me just go ahead and execute this particular code okay.
And let me just create an object.
So I will go ahead and create an object.
And here I will write person person.
And let me just go ahead and give my values crush Chris and 34.
Okay.
And let me just make this spelling curse.
Correct.
So it should be person.
Okay.
Now if I go ahead and check d I r with respect to this particular person.
Now see here you can see that I'm not able to see this particular variable right.
Do you see this or let me do one thing.
Let me just create one more public variable along with this.
So here I will go ahead and write self dot um name age.
Let me just go ahead and say that gender okay.
So gender is equal to I will go ahead and assign this particular gender variable which I'm actually
providing over here.
Okay.
Let's say that this information now here you can see this is a public variable.
And this is a private variable.
Why I'm saying it as a private variable.
Because I'll just tell you in some time okay.
Now let's go ahead and update it over here.
And let's say that I'm writing this is mail.
Okay.
If I go ahead and execute it here you can see directory of person.
Okay.
Uh it takes three positional argument but four were given uh three.
One is name age and uh self dot gender is equal to gender.
Let's see why this is an issue over here.
Self dot gender is equal to gender okay I did not execute this.
Maybe okay.
Now it should work right now Here you can actually see that I am able to see this particular public
variable.
Not I'm not able to see all this underscore name on underscore age below this.
Right.
I'm not able to see it.
Why.
Because that is a private variable.
And wherever private variable will be displayed it will be displayed over here in this format.
Right.
So here you can see when I write underscore person underscore underscore age, the private variable
basically becomes in this particular way right.
It will get appended along with the person class.
Right.
So I'm not getting this direct variable.
Any underscore underscore age is no way present.
So this basically indicates that this is a private variable okay.
Now let me do the same thing.
Let me just go ahead and create a function.
And let me say hey get underscore name.
And here I'm actually going to write uh uh I'm going to give an object called as person.
And I'm just going to say return return person person dot underscore underscore name.
Okay.
I'm just going to return this.
Now let me do the same thing over here okay.
Let me just copy this entire thing.
Let me call this person and let me just go ahead and call this particular function name get underscore
name okay.
And if I go ahead and see this and if I just give this particular parameter will I will I will will
the name get printed.
The answer is no.
Why.
Because it is saying that hey it it has no attribute underscore underscore name.
The reason is very simple because this is a private variable and you will not be able to access this.
You will definitely not be able to access it.
Because if I go ahead and see inside the directory I am able to see that, hey my person, uh, the
underscore underscore age has been in this particular name, right?
Yes.
In this directory you're able to see.
But as a developer you are able to see.
But as a common person you will not know that, hey, my private variable has got converted in this
particular name.
Right?
And that is the reason you are not able to access it.
So here we are restricting right?
What happens with the help of access modifiers or encapsulation?
We are restricting some of the variables because I don't want anyone to see this particular variable
or directly access this variable, because these are some very important attributes in a class which
I should not be changing frequently.
Yes, as a developer, I can definitely see it.
I what I can do, I can just go ahead and copy this and I can print it over here.
Okay.
I can do that as a developer.
But this is not a good practice.
The reason we create a private variable, because it is saying to your fellow developers, hey, this
is a private variable and it should always be remaining hidden.
Now if I go ahead and execute like this, I'll be able to get the answer right.
I'll be able to get a 34 right?
Or if I want to probably display the name, I will be able to get this particular name right.
But if I directly call with underscore underscore name, it will not be possible because I'll be getting
that particular error right.
So what does private variable basically say?
That a private variable cannot be accessed from outside the class.
Very simple.
It cannot be accessed from outside the class.
Can we access it inside the class?
Yes, we can do it.
And for that only will be using getter and setter method.
Right.
So private variable is something like which you cannot access outside the class.
Even you have a derived class that can also not access it.
Okay.
So this is one of the very important property in encapsulation.
And this is where we discuss about private and protected public variable.
There is also one more variable which is called as protected.
How do I define protected.
So protected is nothing.
But I will use the same thing.
And let's say instead of writing double underscore, I'll just write single underscore.
So this is what a protected variable is like, right?
A protected variable or access modifier is like okay, a protected variable basically says that, hey,
you cannot use it outside the class.
Like outside the class basically means we cannot access this underscore name outside the class, okay.
But we can access it from a derived class.
So let me just go ahead and write.
Let's say if I go ahead and say employee is my another class okay.
Employee is my another class.
And if I say hey this is inheriting person.
So here I have created a class employee which is inheriting person.
Now what I'm actually going to do, I'll just go ahead and write my init function.
Right.
So here I'm again going to write my init function.
And inside this particular init function I'm just going to pass all the parameters like let's say age,
gender.
Okay.
And what I'm actually going to do over here, I'll go inside this.
And now I'm going to just write super Dot.
And since I'm inheriting all the attributes from the upper class.
So I will just go ahead and write this self comma name comma h comma gender okay.
Now here you'll be able to see that, uh, if I have this class employee right Right, I should be able
to access this particular values.
Okay.
Uh, and that is what protected variable is all about, right?
Whenever we create a protected variable in one of the classes, only its derived class will be able
to access it.
Other than that, you won't be able to access it outside that particular class.
Okay, so once I probably create this derived class now from this derived class, I should be able to
access all these variables.
So what I will do, I will just remove all these things Okay.
I will not be requiring over here.
Now let me just go ahead and create my employee.
Okay.
And here I'm going to define my employee okay.
And let's say that I'm going to give some values like crush or 34.
And here I'm going to give the gender as male okay.
Now I will just go ahead and print employee dot underscore name.
Right.
So once I execute it here okay five parameters is required.
So let's see which all parameters name age gender.
Uh over here I'm given name age gender.
And here you will be able to see that uh underscore name underscore age.
Um, it takes four positional arguments but five were given.
Let's see where it is.
Four.
Five were given.
Uh, so self is over here.
Employee.
Person is over here.
Employee is over here.
And this is nothing but employee dot underscore name.
Okay guys one mistake that I have done over here I've used self keyword.
So I should not be using this now.
I think it should be displaying it okay.
So employee dot underscore name here you'll be able to see that I'll be able to get this particular
output.
So in the case of whenever we define a protected variable it is it it is just like so it is not anything
is compulsory in Python.
You know, it is uh, like a very uh, it is a kind of programming language where there are no strict
rules, but most of the concepts that we learn.
Right.
Let's say that if I'm trying to create a private variable, if I'm trying to create a protected variable
in case of protected variable, I have to always make sure that this variable will only be accessible
only from the derived class.
In the case of private variable, nothing should be accessible outside the class, you know, and public
variable is what you should be able to see that you will be able to access it from anywhere, right?
So here I have actually shown you the example of all these things, uh, with respect to private variable,
with respect to public variable and all.
And uh, let's say that if I am creating a private variable, if I'm not able to access it from outside
the class, can we access it from methods that are present inside the class?
Okay.
And that is what, uh, we are basically going to do now.
Okay.
And, uh, we are going to learn encapsulation with the help of getter and setter method.
So let's go ahead and write encapsulation with getter and setter okay.
Now what I am actually going to do over here is that, uh, first of all I'm just going to create my
class.
So let me just go ahead and write class person and this will be definition init underscore underscore.
And here I'm going to basically write self name age okay.
And here you can see self dot underscore underscore name.
So this will basically be my private variable I will go ahead and initialize it.
Private access modifier or variable okay.
Or variable or variable.
Okay.
And uh, see it is very simple.
I need to probably not display this anywhere.
It should be restricted within the class itself.
That is what is my aim.
Right then I have self underscore age which will be equal to age itself.
And here I'm going to probably say that this is also my private variable.
Okay.
Now in order to access this particular variable inside the class I can definitely do it.
And for this we'll be using something called as getter method for name.
Okay.
So we'll go ahead and create one function which is called as get underscore name.
And here we're going to use the self keyword.
And with the help of this self keyword wherever we use we should be able to access the underscore underscore
name.
So here I'm going to return self dot underscore underscore name okay.
So this is basically my getter method for name right.
So here you can see that I will just be exposing this particular method to the outside world.
Because this is a public method right.
I will not be exposing underscore underscore name.
With the help of this particular method I should be able to get this right.
Now here is what I will talk about.
One very important thing right.
Have you seen a washing machine.
So if I am probably working on the washing machine in washing machine will be able to see buttons,
will be able to see how to start, will be able to see that where we need to put the clothes right.
But internal working is completely hidden, right?
And that is what over here.
Same thing we are doing.
The internal things are probably hidden over here.
Only the things that matter to us.
You know, just like the function name is available for us or visible to us.
Right?
So here you can see that just by calling this particular method get underscore name, I should be able
to get the uh name itself uh, from this particular function.
This is called as a getter method.
I may also have setter method setter method for name.
Right now this setter method for name.
What it does is that this will help us to change any values that I give to the name.
The name, which is basically a private variable right over here.
So this is my private variable.
I need to probably change that particular value, let's say.
So I can just use self underscore.
Underscore name is equal to name right.
So with the help of getter we are retrieving the data.
With the help of setter we are changing the name itself okay.
Similarly let's say that uh uh I, I want it for the age.
And uh, uh, I can also do that, uh, you know, and probably use a getter method for that and setter
method for that.
So first I will do create a getter method okay.
And here you can see I'm getting the age just writing self dot underscore age.
And now in the case of setter method uh let's say that I want to probably put or change any age.
Right.
So what I do I first of all check the condition.
If age is greater than zero, then only I will go ahead and change it.
Otherwise I'll say age cannot be negative, right?
So this is just one example with encapsulation with uh, getter and setter.
And we are completely hiding the main variables that we have.
Right.
And we are only exposing this particular functions.
That is get underscore name and setter underscore name.
Now let me do one thing.
Let me just go ahead and call all this functions okay.
And then you'll be able to understand.
First of all I'll go ahead and create an object.
Let's say this will be person.
And here I'm going to use crush.
Here I'm going to use 34 okay.
Now you will be able to see here what I'm actually going to do is that access and modify private variables
using getter and setter okay.
This is what we are going to do because obviously we cannot use directly the private variable outside
the class.
Right.
So here I'm going to just print person dot get underscore name.
So if I want to go ahead and display get underscore name I should be able to display it.
Then if I go ahead and print person dot get underscore age.
So I should be able to get that also.
And then I will just go ahead and write person dot set underscore h.
And here I'm going to basically write 35.
Right.
And then I'm going to print uh person dot get age again get underscore age.
So I should be able to see the age now okay.
Yeah.
And then I will go ahead and set the age let's say to minus five so that I get the other error that
I have actually written.
Now, initially I'm creating an object, then I'm probably displaying the name displaying the age.
So crush and 34 will be the answer.
Then when we set the age then it will get replaced with 35.
And then we can actually see this right?
So crush 34.
Then we uh, you can see over here is that, uh, what we are doing?
We have we are printing uh, you are setting this age and then you are printing that particular age.
And then when you are setting it to minus five, you can see that it is showing age cannot be negative,
right.
So this is, uh, what is all about encapsulation.
We have discussed about all the access modifiers like public, private and public, uh, public, private
protected.
You understood that, uh, private variables cannot be accessed outside the class, you know?
So that is the reason we create getter and setter method.
Public variables can be accessed much easily protected.
Needs to be accessed only from the derived class.
But again, in Python nothing is compulsory.
As I showed you when I checked the dir directory of that particular object.
Right?
I should be able to see all the private and public variables, right?
And with just with the help of those name, I can directly call it.
But it is always a good practice that we should follow whenever we are implementing things.
So yes, this was it.
Uh, in the next video we are going to discuss about abstraction.
Thank you.
Take care.
Bye bye.