Hello guys. Welcome to Learnvern.
In the last topic we saw Abstraction where we saw how Abstraction is used in Python, how you can hide the functionalities according to the definition.
Today we will see the last topic in Object Oriented, Encapsulation.
Plus we will also see what Encapsulation is, how to perform it practically & how to use it in Python.
Firstly, what is Encapsulation?
The process of putting variables & methods into 1 class.
For eg: you have made a class and further made multiple variables & multiple methods which can work in the same class, is 1 type of Encapsulation.
I have an illustration here -
We made a class…
Imagine a capsule. Okay. The capsule in medicines.
The cover is a capsule and inside it is the medicinal drugs.
The drugs are variables, methods & capsules are one type of class.
You can call this particular thing an Encapsulation.
There are 2 ways to achieve Encapsulation.
1 is protected which is known by single underscore.
Another is private, which is known by double underscore.
We will now see practically how to achieve it to make it easier for you.
Open Jupyter. I’ll make a class here.
We have to achieve Encapsulation.
I’ll make a class here & I’ll explain protected & private’s meaning in it.
How protected & private Encapsulation work.
I’ll markdown Achieving Encapsulation by Private
We will achieve private Encapsulation first.
Let’s make a class here, class A.
In which I’ll make an Init method that is our Constructor which we saw in class.
We learnt in the Oops method in Class how the Init function works.
I wrote Self here & passed a variable here A.
What I did here was, take an Init function, passed a variable in an argument A and now
Self.__ (dot, double underscore) private. A.
The variable A is a private variable.
I’ll make one more function Def show. Wrote Shelf inside.
I’ll print A. my private variable.
I’ll write down Private variable. Self.__a (dot, double underscore).
Printing a private variable using function.
We will make 1 more class which is class B.
In which I’ll make an Init function in which I passed Self.
And passed a variable.
Now, by using Super Method, our Parent class. This is our Parent Class.
If I want to use the property of the parent class, first of all I’ll have to inherit it. Right?
This is my Child Class.
Now suppose I want to use a particular method, that too a Parent Class Method.
How will I use it? We had used the Super Method in Method Overriding.
If any of you don’t know, you can go to the Method Overriding video to see how the Super Method works.
I used the Super Method here & used the Init method.
We have to pass a variable in the A class that is the Parent Class.
Now I’ll create 1 more function which is showB.
So this function will work for Class B. Then write Self.
Now I’ll print A. I printed A here in this way.
Now we will make an object of Child Class.
Creating an object of Class B.
I took an object here Obj. Class B. Then we will pass an argument in the Init function.
Suppose 20. Then I called the Obj.showB method.
The private variables will work under the same class only.
It’ll show an error as soon as I run it.
See I run it and it displayed an error saying B object has no attribute like this.
Meaning you cannot access the class A property through a Class B object.
So this is how the private Encapsulation is achieved.
That is that the private variable cannot be used in any other class.
You cannot accept the Parent Class property from the other class object.
Now in the exact same way, I’ll markdown here.
It’s time for Achieving Encapsulation in Protection.
I’ll copy paste & edit Protected here.
I’ll make a class A here. In which I’ll make a Def Init function.
That is the constructor of Class A. Self. Took an A variable again.
self._(single underscore) A.
We had taken double underscore here so it was private.
But we took a single underscore here & passed A.
I’ll write here, this variable A is a protected variable.
In the same way, I made a method Def show & wrote Self.
And print Protected variable. Self._a.
I’ll make a Class B here which is our Child Class.
In which I inherited Class A.
Again I made an Init function for Class B.
Self, B. By using the Super Method, the upper…
That is our Parent Class, Class A.
This is our Parent Class.
And this is our Child Class.
By using Super Method, I called the Parent class Init function and passed the value of B.
I’ll make one more method here which is Def showB. Self.
I’ll print this method Variable value.
Now how will we print? I’ll pass Self._a.
Our Child Class is B & we always make the object of Child Class.
So Creating an object of Class B.
I’ll make an object named Obj of Class B and I passed 30 here.
Now, we have to call the obj.showB method.
The variable 30 which we passed, didn’t print under Private but here, it got printed.
Why so? Because Protected variables can be accessed in other classes.
Protected variables can be accessed in other classes. Clear?
This is how you can use Encapsulation in a Protected way as well. Okay?
So what did we see today?
We saw Achieving Encapsulation by Private in which we saw that we cannot access the property of the Parent Class through other Class Objects.
Then we saw Achieving Encapsulation by Protected.
Where we made A a protected variable & then we called it through Class B object.
And lastly we printed it.
So this was Encapsulation.
In the next video we will see Regular Expression.
So see you in the next video.
Share a personalized message with your friends.