โšก OBJECT-ORIENTED PROGRAMMING

OOP structures software around objects โ€” units that combine data (attributes) and behaviors (methods). This mirrors real-world entities, making systems more modular, reusable, and maintainable.

๐Ÿ›๏ธ THE FOUR PILLARS

๐Ÿ“ฆ ENCAPSULATION ๐Ÿงฌ INHERITANCE ๐Ÿ”„ POLYMORPHISM ๐ŸŽญ ABSTRACTION
๐Ÿ“ฆ ENCAPSULATION
Bundle data and methods, restrict direct access. Protects internal state.
class BankVault:
    def __init__(self):
        self.__gold = 0
    
    def add_gold(self, amount):
        if amount > 0:
            self.__gold += amount
    
    def get_gold(self):
        return self.__gold
๐Ÿงฌ INHERITANCE
Derive new classes from existing ones, forming hierarchies.
class Appliance:
    def __init__(self, brand):
        self.brand = brand

class WashingMachine(Appliance):
    def wash(self):
        return f"{self.brand} washing..."
๐Ÿ”„ POLYMORPHISM
Same method name, many forms. Objects decide specific behavior.
def sound(animal):
    print(animal.talk())

class Duck:   def talk(self): return "Quack"
class Frog:   def talk(self): return "Ribbit"
๐ŸŽญ ABSTRACTION
Hide complex internals, present simple interface.
from abc import ABC, abstractmethod

class Switch(ABC):
    @abstractmethod
    def flip(self): pass

class LightSwitch(Switch):
    def flip(self): return "๐Ÿ’ก toggled"

๐Ÿงฑ CORE CONCEPTS โ€” FULL WIDTH, NO BOXES

๐Ÿ“‹ CLASS
Blueprint describing attributes and methods.
class Robot:
    def __init__(self, name):
        self.name = name
        self.battery = 100
    
    def work(self):
        self.battery -= 10
๐Ÿ”ฎ OBJECT (INSTANCE)
Specific entity created from a class, with own state.
r2d2 = Robot("R2")
c3po = Robot("C3PO")
r2d2.work()
๐Ÿ“Š ATTRIBUTES
Variables storing object data (instance or class level).
class Employee:
    company = "OpenAI"   # class attribute
    def __init__(self, name):
        self.name = name   # instance attribute
โš™๏ธ METHODS
Functions inside class; define behavior.
class Timer:
    def __init__(self):
        self.seconds = 0
    def tick(self):
        self.seconds += 1
    @staticmethod
    def info():
        return "simple timer"

๐Ÿฆ EXTENDED OOP EXAMPLE โ€” LIBRARY (NO CONTAINER)

class LibraryItem:
    def __init__(self, title, item_id):
        self.title = title
        self.item_id = item_id
        self.__checked = False

    def borrow(self):
        if not self.__checked:
            self.__checked = True
            return f"โœ“ {self.title} borrowed"
        return f"โœ— {self.title} in use"

    def give_back(self):
        self.__checked = False
        return f"โœ“ {self.title} returned"

    def available(self):
        return not self.__checked

class Book(LibraryItem):
    def __init__(self, title, item_id, author):
        super().__init__(title, item_id)
        self.author = author

    def __str__(self):
        return f"๐Ÿ“– {self.title} by {self.author}"

class Member:
    def __init__(self, name, mid):
        self.name = name
        self.mid = mid
        self.holds = []

    def take(self, item):
        res = item.borrow()
        if "โœ“" in res:
            self.holds.append(item)
        return res

    def replace(self, item):
        if item in self.holds:
            res = item.give_back()
            self.holds.remove(item)
            return res
        return f"{item.title} not held"

    def current(self):
        return "\n ".join(str(i) for i in self.holds) or "nothing borrowed"

book1 = Book("Sapiens", "B101", "Harari")
book2 = Book("Dune", "B102", "Herbert")
member = Member("Lena", "L001")
print(member.take(book1))
print(member.take(book1))
print(member.take(book2))
print("\n๐Ÿ“š " + member.current())
print(member.replace(book1))
print(member.current())

๐Ÿ“ˆ WHY OOP? ยท KEY ADVANTAGES โ€” BORDERLESS

โ™ป๏ธ REUSABILITY
Inheritance & composition avoid duplicate code.
๐Ÿ”ง MAINTAINABILITY
Isolated changes, easier debugging.
๐Ÿ›ก๏ธ SECURITY
Private fields + controlled access (encapsulation).
๐Ÿงฉ MODULARITY
Objects as independent units, team-friendly.
๐ŸŒ REAL-WORLD MAPPING
Design matches domain concepts.
๐Ÿ“ฑ FLEXIBILITY
Polymorphism allows easy extension.

๐ŸŒ LANGUAGE COMPARISON (SYNTAX)

# Python
class Greet:
    def hello(self): return "Hello"

// Java
public class Greet {
    public String hello() { return "Hello"; }
}

// C++
class Greet {
public:
    std::string hello() { return "Hello"; }
};

// JavaScript
class Greet {
    hello() { return "Hello"; }
}

// C#
public class Greet {
    public string Hello() { return "Hello"; }
}

OOP transcends syntax: itโ€™s a mindset. By composing systems from discrete, collaborating objects, we achieve scalability and clarity. This page applies the python programming overview with all types of OOP pillars.for the enthusiastic programmers.

Created By;Glenn Junsay Pansensoy