Object-Oriented Programming (OOP)
OOP organizes code around objects — bundles of data (attributes) and behavior (methods). The four pillars are encapsulation, inheritance, polymorphism, and abstraction.
- Class — a blueprint for creating objects.
- Object — an instance of a class.
- Encapsulation — hiding internal data with private attributes.
- Inheritance — a class can derive from another class.
- Polymorphism — the same method name can behave differently on different classes.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
dog = Dog("Rex")
cat = Cat("Luna")
print(dog.speak())
print(cat.speak())
Try it Yourself →