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.
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.__goldclass Appliance:
def __init__(self, brand):
self.brand = brand
class WashingMachine(Appliance):
def wash(self):
return f"{self.brand} washing..."def sound(animal):
print(animal.talk())
class Duck: def talk(self): return "Quack"
class Frog: def talk(self): return "Ribbit"from abc import ABC, abstractmethod
class Switch(ABC):
@abstractmethod
def flip(self): pass
class LightSwitch(Switch):
def flip(self): return "๐ก toggled"class Robot:
def __init__(self, name):
self.name = name
self.battery = 100
def work(self):
self.battery -= 10r2d2 = Robot("R2")
c3po = Robot("C3PO")
r2d2.work()class Employee:
company = "OpenAI" # class attribute
def __init__(self, name):
self.name = name # instance attributeclass Timer:
def __init__(self):
self.seconds = 0
def tick(self):
self.seconds += 1
@staticmethod
def info():
return "simple timer"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())
# 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.