Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Encapsulation (OOP) quickly and effectively.
212+ students studying
Encapsulation is a core principle of Object-Oriented Programming (OOP) that restricts direct access to an object's data and methods, ensuring that its internal state is only modified in a controlled manner. Encapsulation allows for the creation of a well-defined interface while hiding the internal workings of a class, which enhances data security, modularity, and code maintainability.
_
) or double underscore (__
) in their names to signal restricted access.Here's an example of encapsulation in a BankAccount
class where balance
is kept private and accessed or modified only through getter and setter methods.
class BankAccount:
def __init__(self, account_holder, balance=0):
self.account_holder = account_holder # Public attribute
self.__balance = balance # Private attribute
# Getter method to retrieve balance
def get_balance(self):
return self.__balance
# Setter method to update balance with validation
def set_balance(self, amount):
if amount >= 0:
self.__balance = amount
else:
print("Error: Balance cannot be negative.")
# Method to deposit funds, with private balance access
def deposit(self, amount):
if amount > 0:
self.__balance += amount
print(f"${amount} deposited. New balance: ${self.__balance}")
else:
print("Error: Deposit amount must be positive.")
__balance
is private, meaning it can't be accessed or modified directly from outside the class.get_balance()
provides controlled access to __balance
.set_balance()
allows updates to __balance
only if the new amount is non-negative, preventing invalid assignments.deposit()
updates __balance
using internal logic, allowing positive deposits while protecting __balance
from external modification.self
: Forgetting to use self
when accessing attributes within a class can result in errors and confusion.Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!
70 flashcards
Flashcards on Encapsulation (OOP)
Revise key concepts with interactive flashcards.
Try Computer Science Flashcards7 quizzes
Quizzes on Encapsulation (OOP)
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Encapsulation (OOP)
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Encapsulation (OOP)
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Encapsulation (OOP)
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Encapsulation (OOP) to Deepen Your Understanding and Improve Your Mastery
Join 500,000+ A-Level students using SimpleStudy...
Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!
Report Improved Results
Recommend to friends
Students Supported
Questions answered