QuickDevGuide

Home › OOPs

Encapsulation — OOPs

Encapsulation is the process of wrapping data and methods together into a single unit (class), while protecting data using access modifiers. It achieves data hiding — variables are declared as private and accessed only through public getter and setter methods. Why Encapsulation? Without encapsulation, any part of the code can modify an object's internal state — leading to unpredictable bugs. By making fields private and exposing controlled access via public methods (getters/setters), you enforce validation rules and maintain data integrity. For example, a BankAccount class should never let external code set the balance directly. Instead, deposit() and withdraw() methods validate amounts, check for overdrafts, and maintain an audit trail. Real-World Analogy An ATM machine hides internal…

Key Takeaways