Object-Oriented Programming
Class Notes: Object-Oriented Programming (OOP)
---
# Object-Oriented Programming (OOP)
## What is OOP?
- A programming paradigm based on "objects": bundles of data and behavior.
- Makes code modular, reusable, and easier to maintain.
## Key Concepts
- **Class:** Blueprint for creating objects. Defines attributes (data) and methods (behavior).
- **Object:** An instance of a class.
- **Encapsulation:** Hides internal state, exposing only what is necessary.
- **Inheritance:** Allows new classes to reuse, extend, or modify existing code.
- **Polymorphism:** Different classes can define the same method in their own way.
## Example
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def speak(self):
return "Woof!"
animals = [Animal(), Dog()]
for a in animals:
print(a.speak())
- Shows **polymorphism**: same `speak` call works on different types.
## Benefits
- Reuse: Inheritance allows building on existing code.
- Maintainability: Changes in one class can ripple safely.
- Abstraction: Hide complexity behind interfaces.
## Common Pitfalls
- Overengineering: Don't force everything into classes.
- Tight coupling: Classes depending too heavily on each other.