Encapsulation in Practice
Prerequisites: Interfaces
Revisiting Properties (Level 1) with real validation, exactly as Java's encapsulation lesson revisited getters/setters:
class BankAccount {
private double _balance;
public double Balance {
get => _balance;
private set {
if (value < 0) {
throw new ArgumentException("Balance cannot be negative");
}
_balance = value;
}
}
public void Deposit(double amount) {
if (amount > 0) {
Balance += amount;
}
}
}
A private set lets outside code read Balance but only this
class can change it — enforcing that every change goes through
validated methods like Deposit.