Object-Oriented Programming (OOP) and Structured Programming (SP) are two different paradigms, each with distinct principles for organizing and designing code.
1. **Object-Oriented Programming (OOP)**:
- **Concept**: OOP focuses on objects that represent real-world entities. These objects are instances of classes and encapsulate both **data (attributes)** and **methods (behaviors)**.
- **Core Concepts**:
- **Encapsulation**: Data and methods are grouped into a single unit (the class).
- **Inheritance**: A class can inherit methods and attributes from another class.
- **Polymorphism**: Objects of different classes can share the same method name but perform different actions.
- **Abstraction**: Hides complex implementation details and only exposes essential features.
- **Example**: Consider a `Car` class that defines attributes like `speed` and `color`, and methods like `accelerate()` and `brake()`. A subclass `ElectricCar` could inherit these features and add more specific methods like `charge()`.
2. **Structured Programming (SP)**:
- **Concept**: SP follows a top-down approach, focusing on breaking the program into smaller sub-problems or functions. The program flow is sequential and linear, emphasizing clarity and simplicity.
- **Key Characteristics**:
- The use of **functions** (procedures) to divide the problem.
- A linear flow of control using **loops**, **conditionals**, and **function calls**.
- **Example**: A program that calculates the sum of numbers would have a function `add()` that takes two numbers as input and returns their sum.
3. **Major Differences**:
- OOP organizes code around objects and promotes reusability and scalability, making it suitable for large, complex systems.
- SP organizes code around functions and linear control flow, making it easier to understand for small programs but less flexible for larger systems.