Unit: Unit 7: Introduction to Classes • Interactive Practice
Interactive Python programming tutorial and practice in your browser.
# Conceptual overview: Demonstrating the 4 Pillars in a Banking System
# 1. Encapsulation: State (balance) and operations (deposit) live together
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self._balance = balance # Protected internal state
def deposit(self, amount):
if amount > 0:
self._balance += amount
def get_balance(self):
return self._balance
# 2. Abstraction: The caller uses simple deposit() without worrying about internal accounting
account = BankAccount("Alice", 1000)
account.deposit(500)
print("Account Owner:", account.owner)
print("Updated Balance:", account.get_balance())