3.1 Encapsulation: Data Hiding via Class Capsule
Encapsulation is the bundling of data (variables) and the methods that operate on that data into a single autonomous unit (the class), while shielding the internal state from arbitrary external tampering.
External consumers are forbidden from modifying instance variables directly; they must interact through formal, controlled public access points (getters and setters), where validation rules, logging, and security checks are enforced.
Figure 3.1: Private state is encapsulated inside the class barrier; callers are constrained to interacting exclusively through validated public methods.
3.2 Abstraction: Essential Features vs Hidden Detail
Abstraction is the design technique of exposing only the essential, high-level features of an entity to the outside world while concealing background implementation complexity.
In real life, a driver operates a car using a simple steering wheel, accelerator, and brake pedal (the abstract interface), without needing to understand fuel injection ratios, gear differential mechanics, or anti-lock braking electronics (the hidden complexity).
Figure 3.2: Abstraction separates the public interface from the underlying operational machinery.
3.3 Encapsulation vs Abstraction: Critical Comparison
A classic placement interview question asks: "What is the fundamental difference between Encapsulation and Abstraction?"
| Attribute | Encapsulation | Abstraction |
|---|---|---|
| Core Philosophy | Hiding Data: Prevents unauthorized direct mutation of internal state. | Hiding Complexity: Exposes what an entity does, concealing how it accomplishes it. |
| Implementation Level | Implementation / Code level (bundling fields and functions). | Architectural / Design level (defining blueprints and interfaces). |
| Mechanism | Access modifiers (private, protected) + Getters / Setters. |
Abstract classes and Interfaces. |
| Focus | Security, integrity, and strict containment. | Simplicity, clarity, and decoupling. |