0% found this document useful (0 votes)
2 views

Lecture 3 OOP Encapsulation

Uploaded by

Muwanga Habibu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 3 OOP Encapsulation

Uploaded by

Muwanga Habibu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

YMCA COMPREHENSIVE INSTITUTE

Object Oriented Programming

LESSON 3_2
Encapsulation

© YCI – 2025.
BIT 1203 04/14/2025
What is Encapsulation?
2

 Definition:
 Encapsulation is the bundling of data and
methods within a class to protect data
integrity.
 Key Benefits:
 Data hiding
 Improved security
 Better code maintainability
 Prevents accidental modification of critical
data

© YCI – 2025. 04/14/2025


Access Modifiers in Python
3

 Python does not enforce strict access


control but follows conventions:

Access Syntax Description


Level
Public self.var Can be accessed anywhere
Protected _self.var Should not be accessed outside the
class (by convention)
Private __self.var Cannot be accessed directly outside the
class

© YCI – 2025. 04/14/2025


Public Attributes Example
4

 class Car:
 def __init__(self, brand):
 self.brand = brand # Public
attribute
 def display(self):
 return f"Car Brand: {self.brand}"

 car = Car("Toyota")
 print(car.brand) # ✅ Accessible
© YCI – 2025. 04/14/2025
Protected Attributes Example
5

 class Car:
 def __init__(self, brand, speed):
 self.brand = brand
 self._speed = speed # Protected
attribute

 car = Car("Toyota", 200)


 print(car._speed) # ⚠️Accessible but not
recommended

© YCI – 2025. 04/14/2025


Private Members Example

 class BankAccount:
 def __init__(self, balance):
 self.__balance = balance # Private
 def get_balance(self):
 return self.__balance
 account = BankAccount(1000)
 print(account.get_balance()) # ✅
Allowed
 # print(account.__balance) ❌ Error
© YCI – 2025. 04/14/2025
Getters and Setters
7

 To access private attributes safely, we use getter


and setter methods:
 class Person:
 def __init__(self, name, age):
 self.__age = age

 def get_age(self):
 return self.__age

 def set_age(self, age):


 if age > 0:
 self.__age = age
© YCI – 2025. 04/14/2025
Using @property Decorators
8

 Python provides a cleaner way to define getters and setters:


 class Employee:
 def __init__(self, salary):
 self.__salary = salary
 @property
 def salary(self):
 return self.__salary
 @salary.setter
 def salary(self, new_salary):
 if new_salary > 0:
 self.__salary = new_salary
 @property acts like a getter
 @salary.setter allows controlled modification

© YCI – 2025. 04/14/2025


Benefits of Encapsulation
9

 Protects Data Integrity


 Prevents unintended modifications
 Encourages Code Reusability
 Well-structured code is easier to reuse
 Improves Readability
 Clearly defines how attributes should be
accessed
 Simplifies Debugging
 Issues are easier to track

© YCI – 2025. 04/14/2025


Summary & Best Practices

 Use **private** attributes to restrict


direct access
 Use **getters and setters** for
controlled access
 Use **@property decorators** for
Pythonic approach
 Follow naming conventions and avoid
name mangling unless necessary

© YCI – 2025. 04/14/2025


Thank you

© YCI – 2025. 04/14/2025

You might also like