Python Encapsulation
What is meant by Encapsulation?
The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation.
The motive of Encapsulation: Preventing accidental modifications of data.
Comparing data encapsulation with real-time examples:
Consider a Mobile phone. We use all the features that are involved or embedded within the phone. We access all these features for different purposes and different implementations. But the access is restricted for changing or modifying most features.
In the same way, we use much software which functions with many software tools, but any of those features or software tools cannot be accessed or modified. The access is restricted for a purpose. If there is any scope to access these internal features of the software, we might make some modifications or changes accidentally. These modifications might lead to the destruction of the software, followed by the destruction of the device.
To prevent all such types of issues, the encapsulation concept is implemented. Encapsulation allows you to get more control over the code by capsulizing or combining the code so that small implementations can be done without affecting the remaining code. It protects the data using private variables and private methods. Along with giving protection, it wraps the complete data into a single unit.
In order to implement Encapsulation within the code, we generally use Private Attributes. Let us have a look at those attributes and understand in what way they are used.
Private Attributes:
The attributes that can be either accessed or modified only by using that particular object’s method and can be accessed neither from outside the class nor within the derived class are known as Private Attributes.
Generally, Private Attributes start with " single underscore ", i.e., " _ ". To represent that the given attribute is private, its name should be started with a single underscore or “_”.
Syntax:
self._attributename = “ Hello “
Example program where Private Attributes are used for implementing Encapsulation:
class A:
def __init__ (self):
self.a=20
self._b=40
class B(A)
def __init__ (self):
A.__init__(self):
print(“The value of private attribute present in the Base class A is”)
print(self._c)
obj1 = A()
print(obj1.b)
Output:
AttributeError: ‘A’ object has no attribute ‘b’
Explanation:
Let class A be the Base class and class B be the Derived class. As we know, the private attribute " _b " is declared in the Base class, i.e., class A. Class B is the derived class of A. So, all methods, variables and attributes ( excluding private ) present in the Base class A can be accessed by the objects declared in Derived class B. An object " obj1 " is created in the Base class A. The print statement is passed to print the value of attribute "b". As attribute b is private, it is unable to recognize general attribute b but searching for the existing private attribute “ _b “. That’s the reason for getting an Attribute error in the output.
Let us modify the line that caused an error and try the same example again.
class A:
def __init__ (self):
self.a=20
self._b=40
class B(A)
def __init__ (self):
A.__init__(self):
print(“The value of private attribute present in the Base class A is”)
print(self._c)
obj1 = B()
print(obj1._b)
Output:
The value of the private attribute present in the Base class is 40
40
40
Explanation:
Let class A be the Base class and class B be the Derived class. The private attribute " _b " is declared in the Base class, i.e., class A. Class B is the derived class of A. So, all methods, variables and attributes ( excluding private ) present in the Base class A can be accessed by the objects declared in Derived class B. Let’s change the object creation from A to B. An object “ obj1 “ is created in the Derived class B. The print statement is passed to print the value of the private attribute or member “_b". As attribute b is private, we had mentioned an underscore before the name and stated the attribute to print. Now, we got a successful output without any error representing the passage of the private attribute. We get the same output in the case of private variables and private methods.