What is encapsulation in python with an example? Everything in detailed.


Encapsulation is a way of writing code where we restrict access to methods and variables in such a way that it cannot be modified accidentally.

Why do we use encapsulation?

We use encapsulation so that some variables or methods of class cannot be modified by any public entity.

How do we achieve encapsulation?

We achieve encapsulation using access specifiers. Now, there are three types of access specifiers: private, protected and public.

You may ask what are access specifiers? Access specifiers simply tell us who can modify your variable and from where he can modify it.

Encapsulation with example.

Suppose you have a computer in your house which is your private property which belongs only to you and remains only in your house, no one outside your house can access it. But one day your friend decides to access it, he simply cannot access it because it is in your house, so he decided to communicate with you first because you are directly connected to your computer and only you can access it.

So, you provided a passage or say in a manner in which he can access your computer remotely using some softwares (Team viewer, AnyDesk). 

Here your computer is a private variable or method.

You are a reference between private property and public property who connect both.

Your friend is a public user who wants to access private variables.

Core concept behind encapsulation.

There are three types of variables inside the class: public, protected which is written with single underscore and private which is written with double underscore.

Now, these underscores act as access specifiers which tells us where we can access these variables. A variable without any underscore can be accessed outside class directly, A variable with single underscore can access outside class directly, A variable with double underscore that is private variable cannot be accessed directly outside class, to access these private variables we need another variable which acts as bridge between private variable and public object.

Encapsulation Program in python



As you can see, we have a class called Demo and inside that we have three variables.

We can access public and protected variable with object of class (b is object of class) directly and we are getting respected output, but we cannot access private variable outside class directly, therefore we are getting AttributeError

To solve or overcome this issue we will declare another variable which acts as a bridge between object (b) of class and private variable (__z).



You can Just Copy This.

class Demo:
x = 20 #Public
_y = 30 #Protected
__z = 40 #Private
a = __z
d = Demo()

Now you can see ‘a’ is acting as bridge between __z and object of class b. This is how encapsulation works. Where we restrict direct modification of variable outside class.

Encapsulation using getter and setter methods.

The above example was the basic idea behind encapsulation, the real-life example of encapsulation involves accessing of private variables and modification of those variables with the help of getter and setter methods.

The main idea behind these getter and setter methods is that the getter method will access those private variables and return it as it is. The setter method will change the value of the private variable.



class Demo:
x = 20
_y = 30
__z = 40

def getx(self):
return Demo.__z

def setx(self, x1):
Demo.__z = x1

d = Demo()

Our private variable __z is being accessed with the help of class Demo inside getx() method where nothing is done except, we are returning the value of __z as it is.

Inside method setx() we are bringing a value from object of class (b) and assigning it to private variable __z.

On line 13th we are simply printing the value private variable __z.

On line 14th we are passing a value inside the setx() method which will update the __z variable.

On line 15th we are again printing the updated value of __z.

Method encapsulation.

Till here everything was variable encapsulation there is one more type of encapsulation which is called method encapsulation which is nothing different than this. All we have to do is declare a private method with double underscore and assign another method which will act as a bridge between private method and object of class.

Method encapsulation


As you can see, we have declared method1 as public which we can access outside class directly. But method2 is private which we cannot access outside class thus getting AttributeError. 

To overcome or avoid this issue we will declare a method3 and inside that method we will call our private method that is method2.

Python Encapsulation


class Demo:
x = 20
_y = 30
__z = 40

def method1(self):
print(‘Hello I am public method’)

def __method2(self):
print(‘Hello I am private method’)

def method3(self):

d = Demo()


Realted Post:

Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

Your email address will not be published.