What is encapsulation in OOP?
Short answer: Encapsulation is the mechanism of hiding internal details of an object and exposing only necessary functionalities. It helps in protecting data and maintaining control over how it is accessed or modified. Example: A BankAccount class hides its balance and only allows deposit/withdraw operations: private decimal balance;
Example code
public void Deposit(decimal amount) { if(amount > 0) balance += amount; }
Real-world example (ShopNest)
ShopNest’s Order keeps _items private and exposes AddItem() so totals stay correct—callers cannot put a negative quantity directly into the list.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png