Encapsulation
Keeping an object's data private so it can only change through the object's own methods.
What is Encapsulation?
Encapsulation means keeping an object's data private and only letting outside code change it through the object's own methods. A bank account doesn't let you set the balance directly; you must call Deposit or Withdraw, which can check the rules.
public class BankAccount
{
private decimal balance; // hidden
public void Deposit(decimal amount)
{
if (amount <= 0) throw new ArgumentException("Deposit must be positive");
balance += amount;
}
}
Level: Apprentice · More OOP terms at this level
Updated