Py15:Encapsulation

Encapsulation is process of wrapping attributes and methods together as a single unit. The attributes are well hidden from outside class only only way to access them is through methods in that class.

class Account:
    def __init__(self, name, balance):
        self.name = name
        self.balance = balance

    def info(self):
        print(self.name, end = " ")
        print("Balance is", self.balance)

acc1 = Account("Bank 1", 12)
acc2 = Account("Bank 2", 45)

acc1.info()
acc2.info()

Output

Bank 1 Balance is 12
Bank 2 Balance is 45

Access control

By default, attributes are public can be accessed from anywhere.

A protected attributes can only be accessed by the class itself or from its derived class.

A private attribute can only be accessed from within the class.

To specify the protected accessibility of a attribute, python don't support by rather uses underscore to differentiate from other attributes.

_attribute_name

To implement encapsulation, we have to make attributes as private. To specify attribute as private, python uses double underscore to differentiate from other attributes.

__attribute_name

Example

class Account:
    def __init__(self, name, balance):
        self.name = name
        self.__balance = balance

    def info(self):
        print(self.name, end = " ")
        print("Balance is", self.__balance)

acc1 = Account("Bank 1", 12)
acc2 = Account("Bank 2", 45)

acc1.info()
acc2.info()

print(acc1.__balance)

Output

Bank 1 Balance is 12
Bank 2 Balance is 45
Traceback (most recent call last):
  File "<string>", line 17, in <module>
    print(acc1.__balance)
AttributeError: 'Account' object has no attribute '__balance'

Get and Set

To access the private attribute, either to set new value or to retrieve stored value in a an attribute, a pairs of public methods known as getter and setter methods is used.

The setter method, set's a new value to the attribute. It takes a parameter and a assignment statement in it. The method name begins with the word set and followed by the name of the attribute it want to mutate.

def set_attributeName(self, value):
    attribute = value

Next the getter method, returns value store in the attribute. The method name starts with the word get and followed by the name of the attribute we want to retrieve.

def get_attributeName(self):
    return attribute

Example, the balance attribute has a pair of setter and getter method.

class Account:
    def __init__(self, name, balance):
        self.name = name
        self.__balance = balance

    def info(self):
        print(self.name, end = " ")
        print("Balance is", self.__balance)

    def setBalance(self, balance):
        self.__balance = balance

    def getBalance(self):
        return self.__balance

acc1 = Account("Bank 1", 12)

acc1.info()

acc1.setBalance(30)

print("New balance", acc1.getBalance())

Output

Bank 1 Balance is 12
New balance 30

Comments

Popular posts from this blog

Drawing Simple Pie Chart

VB.net connecting to SQL Server

VB.net