Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧠 Objective
In this chapter, you’ll learn how to:
Interviewers use problems like this to test your
understanding of data hiding, access control, and encapsulated logic,
which are fundamental in designing secure and maintainable systems.
📌 Problem Statement
Build a BankAccount class with the following requirements:
🔧 Step-by-Step
Implementation
✅ Step 1: Create the Class with
Private Variables
class
BankAccount:
def __init__(self, holder_name,
initial_balance=0):
self.__account_holder = holder_name
self.__balance = initial_balance
✅ Step 2: Add Getter and Setter
Methods
def get_balance(self):
return self.__balance
def set_holder(self, new_holder):
if isinstance(new_holder, str) and
new_holder.strip():
self.__account_holder = new_holder
else:
raise ValueError("Invalid
account holder name.")
✅ This enforces controlled access
to the internal state
✅ Step 3: Add Deposit and
Withdraw Logic
def deposit(self, amount):
if amount > 0:
self.__balance += amount
else:
raise ValueError("Deposit must
be a positive amount.")
def withdraw(self, amount):
if amount > self.__balance:
raise ValueError("Insufficient
funds.")
if amount <= 0:
raise ValueError("Withdrawal
must be positive.")
self.__balance -= amount
✅ Checks and balances to avoid
negative transactions
✅ Step 4: Add __str__ for Output
def __str__(self):
return f"Account Holder:
{self.__account_holder}, Balance: ${self.__balance:.2f}"
✅ Full Working Code
class
BankAccount:
def __init__(self, holder_name,
initial_balance=0):
self.__account_holder = holder_name
self.__balance = initial_balance
def get_balance(self):
return self.__balance
def set_holder(self, new_holder):
if isinstance(new_holder, str) and
new_holder.strip():
self.__account_holder = new_holder
else:
raise ValueError("Invalid
account holder name.")
def deposit(self, amount):
if amount > 0:
self.__balance += amount
else:
raise ValueError("Deposit must
be a positive amount.")
def withdraw(self, amount):
if amount > self.__balance:
raise ValueError("Insufficient
funds.")
if amount <= 0:
raise ValueError("Withdrawal
must be positive.")
self.__balance -= amount
def __str__(self):
return f"Account Holder:
{self.__account_holder}, Balance: ${self.__balance:.2f}"
🧪 Example Usage
account1
= BankAccount("Alice", 500)
account1.deposit(200)
account1.withdraw(100)
print(account1)
print("Balance:",
account1.get_balance())
account1.set_holder("Alice
Cooper")
print(account1)
✅ Output:
Account
Holder: Alice, Balance: $600.00
Balance:
600
Account
Holder: Alice Cooper, Balance: $600.00
🔐 Encapsulation Concepts
Table
Concept |
Code Example |
Description |
Private Attribute |
self.__balance |
Cannot be accessed directly from outside |
Getter Method |
get_balance() |
Safely retrieves a private value |
Setter Method |
set_holder() |
Validates and updates internal data |
Data Hiding |
__account_holder not accessible directly |
Prevents misuse or accidental modification |
🔄 Advanced: Use Python
@property Decorators
@property
def
balance(self):
return self.__balance
@balance.setter
def
balance(self, value):
raise AttributeError("Balance cannot
be set directly.")
✅ This makes access cleaner and
Pythonic.
✅ Summary Table
Feature |
Purpose |
__init__ |
Initialize private account details |
__balance |
Private attribute; hidden from external code |
deposit() |
Adds validated amount to balance |
withdraw() |
Deducts with check for sufficient funds |
get_balance() |
Controlled access to sensitive data |
set_holder() |
Allows name change only with validation |
A class is a blueprint for creating objects. It defines attributes (variables) and methods (functions) that describe the behavior of the objects.
Class variables are shared across all instances of a class, whereas instance variables are unique to each object.
__init__() is the constructor method in Python that gets called automatically when a new object is instantiated.
__str__() returns a user-friendly string representation of the object, while __repr__() returns a more technical, unambiguous string for developers.
Python allows a class to inherit from another using (BaseClassName) syntax. The child class gets access to the parent’s attributes and methods.
Encapsulation is restricting direct access to some of an object’s components. This is done using private attributes and getter/setter methods.
Python does not support multiple __init__ methods. However, you can use default arguments or @classmethod to simulate multiple constructors.
Polymorphism allows methods to have the same name but behave differently depending on the class or object calling them
There's no real difference. "Object" and "instance" are often used interchangeably. An object is an instance of a class.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)