Programming Paradigms
- a high-level way to structure computer programs.
- Imperative: Code directly controls execution and state changes.
- Procedural: create procedures/functions and data as inputs.
- Object-Oriented (OO): Organized around "objects" combining data and behavior.
- Class-based OO: Inheritance uses predefined classes.
- Prototype-based OO: Inheritance involves cloning existing objects.
- Declarative: Code describes what to achieve, not how to do it.
- Functional: piping functions to achieve tasks.
- Reactive: creating data streams and focus on propagating data changes
Object-Oriented Programming (OOP)
OOP is a programming model where logic revolves around objects, their characteristics (state), and behaviors (methods).
Building Blocks
- Class:
- A user-defined blueprint for creating objects
- Classes are stored in HEAP memory and support abstraction. Structures are saved in stack memory and do not.
- Object:
- fundamental unit of OOP, represent an instance of a class with its own state, methods, and unique identity.
- Objects communicate via message passing.
- Association: "HAS-A" relationship.
- Aggregation: one object "has many" of another (e.g., a teacher has many students).
- Composition: association where a contained object cannot exist without its container.
Core OOP Principles
- Encapsulation:
- binding data and the functions that operate on that data into a single unit (like a class).
- Access Specifiers: Keywords (
public
, protected
, private
) that control the visibility and accessibility
- Inheritance: Allows one class to acquire properties and behaviors from another ("is-a" relationship).
- Polymorphism: The ability of an entity (like a method) to take on many forms or exhibit different behaviors based on context.
- Overloading: Defining methods with the same name but different parameters (compile-time/static binding).
- Overriding: Subclasses redefine methods inherited from a superclass to modify their behavior (runtime/dynamic binding).
- Abstraction: hiding complex implementation details and showing only essential features.
- Achieved through encapsulation, interfaces, and abstract classes.
- Abstract Classes: Classes with at least one abstract method; they cannot be instantiated directly and must be inherited.
