Programming with Python
Unit III
[Link]
Assistant Professor
Department of B Com(CA)
PSGR Krishnammal College for Women, Coimbatore
SYLLABUS-UNIT III
Object Oriented Programming: Classes and Objects –
Constructors – Destructors Getter and Setter Methods –
Encapsulations – Inheritance – Polymorphism – Abstract
Classes and Interfaces.
Encapsulation in Python
Learning Outcomes:
•Define encapsulation and its role in OOP
•Differentiate between public, protected, private
members
•Use getter and setter methods for controlled
access
•Apply the @property decorator for
encapsulation
•Implement encapsulation in real-world examples
Introduction to Encapsulation
Definition: Wrapping data (variables) and methods
(functions) into a single unit (class).
Purpose: Hide details of implementation from outside
world.
Benefit: Protects the data and maintains integrity.
Why Encapsulation is Needed?
Restricts direct access to critical variables.
Prevents accidental modification of sensitive data.
Provides controlled access using getter and setter methods.
Helps in data hiding and security.
Encapsulation in OOP
One of the four pillars of OOP (Encapsulation, Abstraction,
Inheritance, Polymorphism).
Encapsulation in Python achieved by:
Access specifiers
Getter and Setter methods
Property decorators
Access Specifiers in Python
Public Members – accessible from anywhere.
Protected Members (_var) – accessible inside class &
subclasses.
Private Members (__var) – accessible only inside class
(name mangling).
Example – Public Members
class Student:
def __init__(self, name):
[Link] = name # public
s = Student("Arun")
print([Link]) # Accessible
Example – Protected Members
class Student:
def __init__(self, name):
self._name = name # protected
class SubStudent(Student):
def show(self):
print("Name:", self._name)
s = SubStudent("Arun")
[Link]() # Accessible in subclass
Example – Private Members
class Student:
def __init__(self, name):
self.__name = name # private
s = Student("Arun")
print(s.__name) # Error
print(s._Student__name) # Accessible using name mangling
Encapsulation with Getter & Setter
class Student:
def __init__(self, name):
self.__name = name
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
Encapsulation with Validation
class Employee:
def __init__(self, salary):
self.__salary = salary
def set_salary(self, salary):
if salary > 0:
self.__salary = salary
else:
print("Invalid salary")
Encapsulation with Property Decorator
class Student:
def __init__(self, name):
self.__name = name
@property
def name(self):
return self.__name
@[Link]
def name(self, value):
if value != "":
self.__name = value
else:
print("Invalid name")
Cleaner and Pythonic approach
Read-Only Property Example
class Circle:
def __init__(self, radius):
self.__radius = radius
@property
def radius(self):
return self.__radius
Attribute can be read but not modified.
Real-World Example – Bank Account
class BankAccount:
def __init__(self, balance):
self.__balance = balance
@property
def balance(self):
return self.__balance
@[Link]
def balance(self, value):
if value >= 0:
self.__balance = value
else:
print("Invalid balance")
Advantages of Encapsulation
Improved security
Easier maintenance
Avoids accidental data corruption
Flexibility – change implementation without affecting
users
Comparison with Other Languages
• Java/C++ – explicit access specifiers (public, private, protected).
• Python – relies on naming conventions (_ and __).
• Python trusts developers (“we are all adults here”).
Summary
• Encapsulation = data hiding + controlled access.
• Achieved with access specifiers, getters, setters, properties.
• Provides security, validation, flexibility.
• Common in real-world apps: banking, payroll, student systems.
ASSESSMENT-QUIZ
[Link]