The Four Pillars of Object-Oriented Programming:
Object-Oriented Programming (OOP) is a software design paradigm where programs are organized around data (objects) and discrete functions (methods) rather than sequential procedures and logic. Unlike procedural programming (e.g., C or Pascal), OOP models real-world entities into computational units containing state and behaviour.
| OOP Principle | Definition & Conceptual Mechanism | Real-World Analogy & Java Syntax |
|---|---|---|
| Encapsulation | The wrapping up of data (fields/variables) and methods operating on that data into a single unit (Class), restricting direct external access via access specifiers (Data Hiding). | A medical capsule containing medicinal ingredients. In Java, declaring instance variables private and providing public getter and setter methods. |
| Abstraction | The act of representing essential features without including the background details or internal implementation complexities. | Driving an automobile by pressing the accelerator and steering without needing to understand fuel injection or differential gear ratios. In Java, achieved via Interfaces and Abstract Classes. |
| Inheritance | The mechanism by which one class (subclass/child) derives the state and behaviors of an existing class (superclass/parent), promoting code reusability. | A child inheriting physical traits from biological parents. Implemented in Java using the extends keyword (e.g., class Car extends Vehicle). |
| Polymorphism | The ability of a message, function, or method to be displayed in more than one form (derived from Greek: poly = many, morph = forms). | A person behaving differently at home, in the classroom, and in a hospital. In Java, achieved at compile-time via Method Overloading and at runtime via Method Overriding. |
Procedural vs Object-Oriented Paradigm:
- Procedural: Top-down approach, emphasis on functions/algorithms, data moves openly around the system from function to function, low security and difficult to maintain as programs scale.
- Object-Oriented: Bottom-up approach, emphasis on data security, access to data is strictly regulated through class methods, highly modular, reusable, and scalable.