Cyberithub

Concept of Data Encapsulation in Python Explained with Best Examples

Advertisements

Let's look at the Concept of Data Encapsulation in Python. In the previous tutorials we have looked into the Concepts of Inheritance and Polymorphism in Python so in this tutorial we will look into the next concept of Data Encapsulation in Python Explained with Best Examples. If you are a programmer or developer then you might have used this concept in other programming languages like Java, C++, C# etc.

There you must have seen the usage of access modifiers like public or private but in the case of python we do not use it like that. Here we represent access modifiers differently. Also python provides global access to all the variables and methods. You will get more clarity as we go deep into the Concept of Data Encapsulation in Python. Check more on Python Official Documentation.

What is OOPs

OOPs is a unique concept to solve complex day to day programming problems. Like many other programming languages, python also use this concept to solve complex problems. OOPs is basically known as Object Oriented Programming concept where everything is treated like an object. You need to write your programs in the form of a class and then instantiate that class to create an object.

The four important pillars of OOPs are:-

  • Polymorphism
  • Inheritance
  • Data Abstraction
  • Data Encapsulation

What is Data Encapsulation

Data Encapsulation is a technique or concept to protect your data from outside world. When we say data then it means all the program variables and methods and when we say outside world then it means protection from all the externals programs.

Concept of Data Encapsulation in Python Explained with Best Examples

Concept of Data Encapsulation in Python

Also Read: Inheritance Concepts and Its 5 Different Types in Python with Examples

Data Encapsulation

It's a OOPs concept where data (stored in variables) and methods that operate on these data are bind together in a single unit. Hence puts restriction on accessing these data and methods directly which ensures there are no accidental changes made to them while accessing. Real life example would be, we operate television via a remote.

We can increase or decrease volume, change the channel, perform different settings etc. without knowing the internal implementation on these operations. Hence implementation on these operations are hidden from outside world and only allowed to operate via interfaces (which are remote buttons in this case). This is called Data Encapsulation.

In python, there are 3 types of access modifiers. They are:

  • Public
  • Private
  • Protected

Private and Protected access modifiers are represented by __(double underscore) and _(single underscore) respectively.

Private Access Modifier 

The data member and data functions which are declared as protected can only be accessible from within the class as you can see from below program example.

class Petrol:
def __init__(self):
self.__oldprice= 100
self.__newprice= 110

def show(self):
print("Previous petrol price: {}".format(self.__oldprice))
print("Current petrol price: {}".format(self.__newprice))

def setPrice(self, old, new):
self.__oldprice= old
self.__newprice= new

ob= Petrol()
ob.show()
ob.__oldprice= 110   # Value wont be updated as this is private variable
ob.__newprice= 112   # Value wont be updated as this is private variable
ob.show()     # same values are above
ob.setPrice(110,112)
ob.show()    # Now values will be updated

Program Output

[root@localhost ~]# python example.py
Previous petrol price: 100
Current petrol price: 110
Previous petrol price: 100
Current petrol price: 110
Previous petrol price: 110
Current petrol price: 112

Protected Access Modifier 

The data member and data functions which are declared as protected can only be accessible to a class derived from it which can seen through below program example.

class Petrol:

def __init__(self,old, new):
self._oldprice= old
self._newprice= new

class Diesel(Petrol):
def __init__(self, old, new):
Petrol.__init__(self, old, new)

print("Previous petrol price: {}".format(self._oldprice))
print("Current petrol price: {}".format(self._newprice))
ob= Diesel(110,112)

Program Output

[root@localhost ~]# python example.py
Previous petrol price: 110
Current petrol price: 112

 

 

 

Popular Recommendations:-

How to List all the Installed Modules in Linux{2 Easy Methods}

Solved: ModuleNotFoundError No module named "numpy" in Python3

Python3: ModuleNotFoundError No module named "prettytable" in Linux

Solved: ModuleNotFoundError No module named "requests" in Python3

How to Check Stateful and Stateless Pods in Kubernetes Cluster

3 Easy Ways to Check/Find OpenSUSE Linux Version

Migrate CentOS 8 to CentOS Stream 8 in 6 Easy Steps

Leave a Comment