OOP (Object-Oriented Programming) — Encapsulation
Encapsulation is about bundling data and methods that work on that data into a single unit (class). It hides the internal details of how things work and only exposes what is necessary. This is done using access modifiers like private, public, and protected (depending on the programming language). For example, a bank account class might hide the account balance so that it can't be changed directly by the user. Instead, you provide methods like deposit() and withdraw() to safely update the balance. Encapsulation increases security and helps prevent unwanted changes to data.
class BankAccount {
#balance = 0;
deposit(amount) {
this.#balance += amount;
console.log('Deposited: ' + amount);
}
getBalance() {
return this.#balance;
}
}
let account = new BankAccount();
account.deposit(100);
console.log(account.getBalance());