How to distinguish paradigms of object-oriented, functional, and imperative programming, and what are their key differences?
sobes.tech AI
Answer from AI
Programming paradigms differ in approaches to code organization and problem solving:
-
Object-Oriented Programming (OOP) is built around objects — entities that combine data and methods to operate on them. Main principles: encapsulation, inheritance, polymorphism. Example: class
Carwith methodsdrive()and propertiescolor. -
Functional Programming (FP) focuses on pure functions that have no side effects and always return the same result for the same inputs. Uses higher-order functions, immutability, and recursion. Example:
mapfunction for list transformation. -
Imperative Programming describes a sequence of steps that change the program's state. This is a traditional style using loops, conditions, and assignments.
Key differences:
- OOP organizes code around objects and their interactions.
- FP revolves around computations using functions without changing state.
- Imperative is about step-by-step state changes and flow control.
Example in Python:
# OOP
class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1
# FP
def increment(x):
return x + 1
# Imperative
count = 0
count += 1