OOP (Object-Oriented Programming) — with Champak Roy

☀️ Programmer’s Picnic • Champak Roy
ClassBlueprint / design
ObjectReal instance
ConstructorAssembly at birth
MethodsActions / behavior
Your images Big picture Core OOP ideas Code (3 languages) MCQ quiz Assignments

1) Your OOP storyboards (images)

Each image becomes a “chapter” of OOP understanding.

Champ Car Co. is founded — car class components and sub-components
Image 1: Car as a class made from components (composition).
Champ Bank plans its blueprint — BankAccount with components and methods
Image 2: BankAccount as a class with fields + methods (deposit(), withdraw()).
All components made fitted into final product — constructor in OOP
Image 3: Assembly time = constructor (sub-components → components → final object).
Constructor for BankAccount coded and called
Image 4: Constructor code in action: object created, parts fitted, ready to use.
Driver presses brake and pressure transmitted to wheels
Image 5: One action triggers internal work: method call → internal components respond.
Deposit and withdraw actions as UI buttons
Image 6: Two objects/users calling methods: deposit and withdraw.
Coming up next encapsulation polymorphism abstraction
Image 7: What’s next in OOP: Encapsulation, Polymorphism, Abstraction

2) Big picture: OOP in one line

OOP = write programs by modeling real things as objects that have data (fields) + actions (methods).

Car & BankAccount mapping (from your images)

Car (object)
Data: body, engine, wheels, brakes…
Actions: start(), brake(), honk(), accelerate()
BankAccount (object)
Data: accountNo, customer, balance
Actions: deposit(), withdraw(), showBalance()

In your comics, the team first makes a blueprint (class), then breaks it into parts (composition), then assembles (constructor), then uses it by pressing buttons (methods).

3) Class vs Object (super clear)

  • Class = design / blueprint (like “Car (class)” or “BankAccount (class)”).
  • Object = real instance created from the class.
  • Many objects can come from one class (many accounts, many cars).
Quick memory:
Class = recipe • Object = cooked dish

Constructor (from Image 3 & 4)

  • Constructor runs when an object is created.
  • Its job: fit parts together and set starting values.
  • Python: __init__ • Java: ClassName(...) • JS: constructor(...)

4) Core OOP concepts (with simple examples)

Learn the “why”, not just the definitions.

Encapsulation
Keep data protected; allow access only through methods.
Example: balance should not be directly changed; use deposit()/withdraw().
Abstraction
Show only what user needs; hide internal complexity.
Example: “Press brake” — user doesn’t care about oil pressure, pads, discs details.
Inheritance
Reuse and extend: “child” class gets parent features.
Example: SavingsAccount and CurrentAccount share base Account rules.
Polymorphism
Same method name, different behavior depending on object type.
Example: payFee() works differently for Savings vs Current.
Composition (your Car example!)
A car has an engine, wheels, brakes… → “has-a” relationship. This keeps designs clean and real-world.

5) Code examples (Python / Java / JavaScript)

Same idea in 3 languages: BankAccount + composition + safe balance.

✅ OOP: class, object, constructor, encapsulation, composition
# Programmer's Picnic — OOP in Python (Champak Roy)
# Focus: composition + encapsulation + constructor

class Customer:
    def __init__(self, name, age, address, phone):
        self.name = name
        self.age = age
        self.address = address
        self.phone = phone

    def __repr__(self):
        return f"Customer(name={self.name!r}, age={self.age}, phone={self.phone!r})"


class Money:
    # A tiny helper class to show composition (BankAccount HAS-A Money)
    def __init__(self, rupees=0, paise=0):
        total = rupees * 100 + paise
        if total < 0:
            raise ValueError("Money can't be negative at creation.")
        self._paise = total  # internal storage in paise

    @property
    def paise(self):
        return self._paise

    def add(self, rupees=0, paise=0):
        self._paise += rupees * 100 + paise

    def can_subtract(self, rupees=0, paise=0):
        return self._paise - (rupees * 100 + paise) >= 0

    def subtract(self, rupees=0, paise=0):
        amount = rupees * 100 + paise
        if self._paise - amount < 0:
            raise ValueError("Insufficient balance.")
        self._paise -= amount

    def __repr__(self):
        r = self._paise // 100
        p = self._paise % 100
        return f"₹{r}.{p:02d}"


class BankAccount:
    # Encapsulation idea: balance is internal, only changed by methods
    def __init__(self, account_no, customer, opening_balance):
        # Constructor = assembly: fit parts into the final object
        self.account_no = account_no
        self.customer = customer          # composition
        self._balance = opening_balance   # keep it "private-ish"

    def deposit(self, rupees=0, paise=0):
        if rupees < 0 or paise < 0:
            raise ValueError("Deposit must be non-negative.")
        self._balance.add(rupees, paise)
        return self._balance

    def withdraw(self, rupees=0, paise=0):
        if rupees < 0 or paise < 0:
            raise ValueError("Withdraw must be non-negative.")
        self._balance.subtract(rupees, paise)
        return self._balance

    def show_balance(self):
        return self._balance

    def __repr__(self):
        return f"BankAccount({self.account_no}, {self.customer}, balance={self._balance})"


# --- Demo (objects created from classes) ---
cust = Customer("Asha", 20, "Varanasi", "90000-00000")
acct = BankAccount(119827, cust, Money(rupees=5000, paise=0))

print(acct)
acct.deposit(rupees=250)
print("After deposit:", acct.show_balance())
acct.withdraw(rupees=100)
print("After withdraw:", acct.show_balance())

6) MCQ Quiz (interactive)

Attempt, submit, see explanations + score.

Score:

7) Assignments (practice like a pro)

Do these in any language first, then port to the other two.

A1 — “Car Composition” (from Image 1)
  • Create classes: Engine, Wheel, Brake, Horn.
  • Create a Car that “has-a” Engine, 4 Wheels, Brakes, Horn.
  • Add methods: start(), brake(), honk().
  • Print a neat status report of components.
A2 — “BankAccount Rules” (from Image 2, 6)
  • Require minimum balance ₹500 for withdrawal.
  • Allow deposit only if amount > 0.
  • Add transaction history list (last 10 actions).
  • Write test cases: deposit, withdraw, edge cases.
A3 — Abstraction (from Image 5)
  • Create class BrakeSystem with internal steps (private/helper methods).
  • Expose only one public method: applyBrake().
  • Show that user calls one method, but many internal steps run.
A4 — Inheritance + Polymorphism
  • Create base class Account with deposit/withdraw.
  • Make SavingsAccount (interest) and CurrentAccount (overdraft rules).
  • Polymorphism: call withdraw() on a list of accounts and see different behavior.
Mini Project (Programmer’s Picnic ⭐)
Build a tiny console app: create customer → open account → deposit/withdraw → print statement → save statement as text.