High in this session, we will discuss about encapsulation, so encapsulation allows us to restrict
access to attributes and methods of a class by adding double underscored in front of the attribute or
method definition.
So whenever these methods and attributes become internal to class and can be accessed by the class itself
and are not Colleyville outside the class, so let us see.
I have certain methods or which actually modify the salary of the person, and I don't want any external
method or anything else to actually be able to access the salary of a person because it is something
which is related to the security of the person.
It is the privacy of the person.
So what I can do is I can make the salary and the methods which are excessive, the existing the salary
as private.
I can make them private by adding this double in the school in front of the variable or the attribute
which is containing the salary or any variable which I want to make private.
So that is all I can do it.
So let's get further and implement encapsulation.
So here I'll be implementing the same.
So here I'm creating a class employee and I have a book inside this object.
So this object is the old class which I am importing.
So you would have understood by the pattern which is written here.
So here how we write you.
When we are creating a class inside the prisons, we usually provide the best class.
So here we are using object as the base class for this particular class and we will start the definition.
So we see death and we give the unit and we give the self inside this and then we will see says the
name is equal to any name.
See my name.
Then we say says don't.
Each is equal to any age.
Then you say say not double under score.
Salary is equal to the salary of the person, whatever the salary is right now what we do is we are
creating an object of it.
Now this is my class and I want to create an object of this class.
So let's say I create an object, object one or we do one.
And from this object, I want to create an instance of this.
So let me get this and say employ
and I can access the details of this.
I can access from object one dot name for B.G., one dot age so I can print this right.
And I can simply say print and I can print this here again.
I can say print and I will be able to print this again.
So let me love this so you can see I can I'm able to see the name on the image of the person.
Now let me do one more thing.
Let me I've decided.
So now I'm doing the same thing and I'm putting silalahi here.
Now, what happens here is it gives me this error, which is employee object has no attribute, double
endoscopes gallery.
So this is because it is treating it as internal in nature.
It does not want to show it to other objects or any other function or any other method.
So it is saying that it has nothing as a salaried set.
So it is hiding it.
This is called encapsulation.
Encapsulation is basically data hiding.
We are hiding this particular data from any other external sources and we are showing it only to the
class itself.
So any function which is present inside this particular class will be able to access this salary.
But except from that, any other class or any other object will not be able to access this.
So how we can do that?
So let's do one thing.
Let us see how we can modify that.
So for that, let us feed on a.
Examples will be class person and we put in objects again.
Now we see death and now I'm creating see the.
And each time a assigning name and age and I'm creating self and I'm giving names and I'm giving each
next, I will go further and say self-taught name is equal to a name, age old age, self-taught.
Now I'm getting the name and so I'm taking these now let me get this in a private one and let me create
a private function.
OK, so I create a private function.
Let me move this one unclarity and I'm defining another function of this function is again, double
underscored the split and it again, big self.
And what it does is it simply prints
itself, both name and self,
not double under scrutiny.
So this will display the name and age.
And now let me run this from here.
So I thought that will underscore the split.
So I'm running this function from inside and after that I'm seeing it, Don.
OK, now I don't run from this also.
OK.
And after that, I create an object so I can see for one.
This is my first post and this is the person.
So now it will create the person for me.
Further, I see, for one thought, let me copy this so that this.
So now what we have done is we have created the last person in that class person, we have created a
fine name and age which takes name and age, and it assigns it to self and so forth, each leader.
We have created a sense of this, which is actually and private method, which will again bring the
name and age for this.
Now, let us run this.
So as we as this you can see there is no problem in running the design name and age.
Next, we'll try to run the.
This limited affects, so when you say for one.
Not sign.
So it's an anticipated.
So when we're on this, we get an Ed Wood says personal object has more to go.
Now, that is a common thing because it is a private matter.
So now what we do is let us run the letters again, forget the name very clearly.
And this one is, for one, the name.
There's no problem in giving the industry the edge so to for one's
age.
So, again, the same thing, of course, an object has no of.
So this is the property of glasses.
When we use double store in front of any other method of defining it, then it will act as a private
matter.
That is, you cannot access it outside the method and it will be accessible.
You can access it from any other method, so if I want to access it from this, from his name and age,
I am able to access it.
So whenever I've done this particular line, then it is being printed because it is right.
We have just given the assignment to name and age and after that we have uncensored history.
Now, if I don't give this, you can see and I will run this line so we won't give anything.
So this means that it is running the cell door and is being drawn and also it is running from the Internet
itself, but it is not even run it from outside.