Course Content

  • Encapsulation

Course Content

Encapsulation in Python

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.

Thank you.


Encapsulation is the process of combining data and the functions that deal with it into a single object. You can hide the object's internal state from the outside by doing so. This is referred to as information concealment. Encapsulation can be seen in the form of a class.

In Python, encapsulation refers to the principle of grouping data and methods into a single unit. When you construct a class, for instance, you are implementing encapsulation. Encapsulation is demonstrated by a class, which binds all data members (instance variables) and methods into a single unit.

Unlike some other object-oriented languages, Python lacks the private keyword, but encapsulation is possible. Instead, it follows the tradition of prefixing a class variable that should not be directly accessed with an underscore.

Encapsulation is a technique for hiding the values or state of a structured data object within a class, preventing direct access by clients from exposing secret implementation details or violating the methods' state invariance.

Encapsulation is accomplished by defining a class's data members and methods as private or protected. However, direct access modifiers such as public, private, and protected do not exist in Python. We may accomplish this by employing single and double underscores.

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere