Python Encapsulation Concepts
Python Encapsulation Concepts
1. Basic Encapsulation
Encapsulation refers to the bundling of data and methods that operate on that data within a single
unit, or class. It restricts direct access to some of the object's components.
class Employee:
def __init__(self, name, age, salary):
# Private attributes
self.__name = name
self.__age = age
self.__salary = salary
def get_name(self):
return self.__name
def get_salary(self):
return self.__salary
class Employee:
def __init__(self, name, age, salary):
self.__name = name
self.__age = age
self.__salary = salary
def get_name(self):
return self.__name
def get_age(self):
return self.__age
class Account:
def __init__(self, balance):
self.__balance = balance # Private attribute
def get_balance(self):
return self.__balance
acc = Account(1000)
acc.deposit(500) # Deposit 500
print(acc.get_balance()) # 1500
class BankAccount:
def __init__(self, account_number, balance):
self.__account_number = account_number
self.__balance = balance
def get_balance(self):
return self.__balance
class Employee:
def __init__(self, name):
self.__name = name # Private attribute
emp = Employee("Mike")
print(emp._Employee__name) # Access private attribute using name mangling