.NETEasy
What is the difference between abstraction and encapsulation?
Abstraction is about hiding what something does — exposing a simple interface that omits implementation details.
public interface IPaymentGateway {
Task<PaymentResult> ChargeAsync(decimal amount, string token);
}
The caller doesn't know if it talks to Stripe, Braintree, or a mock. That's abstraction.
Encapsulation is about hiding how something works — protecting internal state from outside mutation.
public class BankAccount {
private decimal _balance; // hidden state
public decimal Balance => _balance; // controlled read
public void Deposit(decimal amount) { // controlled write
if (amount <= 0) throw new ArgumentException();
_balance += amount;
}
}
You can't bypass Deposit and assign _balance directly. That's encapsulation.
Quick mnemonic:
- Abstraction → "I don't need to know HOW the payment is processed"
- Encapsulation → "You can't reach in and break my invariants"
Both serve the same goal (reducing accidental complexity) but at different layers — abstraction at the API boundary, encapsulation inside the class.