Object-Oriented Programming (OOP) is a programming paradigm that structures programs around “objects” as fundamental units. This improves code reusability, maintainability, and extensibility.
Components of an Object
Objects model real-world things and concepts, consisting of two elements:
- Field: Represents the data and attributes (state) of an object. Defined as variables.
- Method: Represents the behavior and operations of an object. Defined as functions.
Fields and methods together are called members.
Classes and Instances
- Class: A blueprint or template for objects. Defines what fields and methods an object will have.
- Instance: An actual object created in memory based on a class blueprint.
The operation of creating an instance from a class is called instantiation.
The Three Pillars of OOP
There are three particularly important concepts in object-oriented programming.
1. Encapsulation
A mechanism that hides an object’s internal structure (fields) from direct external access, allowing manipulation only through exposed methods.
- Data hiding: Fields are declared with access modifiers like
privateto prevent direct external modification. - Exposing methods for field manipulation:
publicmethods are provided for setting (setters) and getting (getters) field values.
This maintains object integrity, prevents unintended data modifications, and improves code maintainability.
2. Inheritance
A mechanism that allows a new class (subclass or child class) to inherit fields and methods from an existing class (superclass or parent class).
- Improves code reusability.
- Builds hierarchical structures between classes for systematic management.
3. Polymorphism
Meaning “many forms,” it refers to methods with the same name exhibiting different behavior depending on the object’s type (class).
- Override: Redefining a parent class method in a child class.
- Interface: A mechanism that enforces multiple classes to implement common methods.
Polymorphism improves code flexibility and enables uniform handling of different objects.
UML (Unified Modeling Language)
UML is a standardized notation for visually representing the design and structure of software systems. It is used in various phases of system development.
Common UML diagrams:
- Class Diagram: Represents classes, their attributes, operations, and relationships (inheritance, associations, etc.) within a system.
- Sequence Diagram: Represents the temporal sequence of messages (method calls) exchanged between objects.
- Use Case Diagram: Represents the system’s functions (use cases) and the users (actors) who interact with them.
- Activity Diagram: Represents the flow of a system or business process, including the sequence of activities and conditional branches.
- Component Diagram: Represents the physical components (modules, libraries, etc.) that make up the system and their dependencies.