What is Object-Oriented Programming
Object-Oriented Programming (OOP) is a programming paradigm that organizes software around objects rather than functions and logic. An object combines data (attributes) and behaviors (methods) into a single entity, reflecting the way we think about the real world.
💡 Fundamental Concepts
- Class: Blueprint (model) for creating objects
- Object: Instance of a class with specific data
- Attributes: Data/properties of the object
- Methods: Functions/behaviors of the object
From Procedural to OOP
In procedural programming, data and functions are separate. In OOP, they are encapsulated together, making the code more modular and intuitive.
Procedural Approach:
// Separate data
let userName = "Mario";
let userAge = 30;
// Separate functions
function printUser(name, age) {{ '{' }}
console.log(name, age);
{{ '}' }}
OOP Approach:
// Data and behaviors together
class User {{ '{' }}
name: string;
age: number;
print() {{ '{' }}
console.log(this.name, this.age);
{{ '}' }}
{{ '}' }}
The Four Pillars of OOP
1. Encapsulation
Hides internal details, exposing only what is necessary through access modifiers (private, protected, public).
class BankAccount {{ '{' }}
private balance: number = 0;
deposit(amount: number) {{ '{' }}
if (amount > 0) this.balance += amount;
{{ '}' }}
getBalance() {{ '{' }}
return this.balance;
{{ '}' }}
{{ '}' }}
const account = new BankAccount();
account.deposit(100);
console.log(account.getBalance()); // 100
// account.balance = 999; // ❌ Error: balance is private
2. Inheritance
Allows creating new classes based on existing classes, reusing and extending functionality.
class Animal {{ '{' }}
protected name: string;
constructor(name: string) {{ '{' }}
this.name = name;
{{ '}' }}
move() {{ '{' }}
console.log(`






