Python File handling

Encapsulation in Python

  • Encapsulation is a mechanism where data members and methods are bind or wrap together. For example. we take a class and we put variables and methods in the class, so class is binding variables and methods together, here class is encapsulation.
Encapsulation in Python
Class: Encapsulation in Python
  • The variables and methods of a class is known as members of the class. All the members of class are Public, that means all members are by default available outside the class and any other programs or classes can access it.
  • Python follows Uniform Access Principal for members which means all members of class should be accessible in a uniform manner.
  • We can put restriction on directly accessing class members which can prevent accidental or unauthorized modification of data.
  • The Instance variables can only changed by Instance method to prevent accidental or unauthorized changes. This is also known as Private members of class.
  • Encapsulation isolates the members of a class from the members of another class. That’s the reason we can use same name for members of two different classes. The reason is when we create objects every objects shares different memory so there is no data conflict. This gives advantage to use same name of member in different classes.
  • For example, we can declare ‘id’, ‘name’ in different classes like Person, Employee, Student etc.
class Employee:
    def __init__(self):
        self.id = 8
        self.name = 'Ram'
        #Private Variable
        self.__accountNo = '18CAB9'

     # class methods
    def display(self):
        print("Id = ",self.id)
        print("Name = ",self.name)
        print("Account No = ",self.__accountNo)
        
    def setAccountNo(self,accountNo):
        self.__accountNo = accountNo
   
 
shyam = Employee()
print("Before Modification:")
shyam.display()

print("After Modification:")
shyam.__accountNo = 27
shyam.name = "Shyam"
shyam.display()

print("After Setting Account No with Set method:")
shyam.setAccountNo('23D4S9')
shyam.display()
OUTPUT:

Before Modification:
Id =  8
Name =  Ram
Account No =  18CAB9

After Modification:
Id =  8
Name =  Shyam
Account No =  18CAB9

After Setting Account No with Set method:
Id =  8
Name =  Shyam
Account No =  23D4S9
  • Above Employee class is example of encapsulation because it wraps data member id, name, account number and display() method together.
  • self.id represent employee id and self.name define employee name, both are also known as instance variable and both are public member. id and name are public variable so it can be modify from outside the class. __accountNo is private member of class so it can not be changed from the class. If we want to modify value of account number we need to create method inside the class to update the value of accountNo.
  • In python __(double underscore) is used to declare private member in the class. So, in Employee class __accountNo is private.
  • Class variable name which starts with _(single underscore) are protected members of class. This members of the class cannot be accessible outside the class. It can only be accessible within the class or in its subclasses.
  • display() method displays the instance variables. setAccountNo() is used to set value of account number, it takes two arguments self and accountNo. self represents current instance and accountNo represents new value which we want to assign account number.

Leave a Comment

Your email address will not be published. Required fields are marked *