Today we’ll explore Object-Oriented Programming (OOP).
OOP is not just a way of writing code, it’s a powerful approach to organizing large projects efficiently.
If you’re working on big projects or web applications, OOP will make your work much easier.
With OOP, your code is divided into classes and objects, each having its own responsibility.
class Student {constructor(name, roll) {this.name = name;this.roll = roll;}study() {console.log(`${this.name} is studying`);}}const student1 = new Student("Ripan", 101);student1.study(); // Ripon is studying
You can create a new class from an existing one using inheritance.
class Animal {speak() {console.log("Animal makes sound");}}class Dog extends Animal {speak() {console.log("Dog says: Woof Woof");}}const myDog = new Dog();myDog.speak(); // Dog says: Woof Woof
Encapsulation keeps your data and methods secure from uncontrolled access.
class BankAccount {#balance; // private fieldconstructor(balance) {this.#balance = balance;}deposit(amount) {if (amount > 0) this.#balance += amount;}getBalance() {return this.#balance;}}const account = new BankAccount(5000);account.deposit(1000);console.log(account.getBalance()); // 6000
Methods with the same name can behave differently across objects.
class Bird {sound() {console.log("Bird makes sound");}}class Parrot extends Bird {sound() {console.log("Parrot says: Hello!");}}class Crow extends Bird {sound() {console.log("Crow says: Caw Caw!");}}const parrot = new Parrot();const crow = new Crow();parrot.sound(); // Parrot says: Hello!crow.sound(); // Crow says: Caw Caw!
Imagine you are building a Car Rental System.
class Car {constructor(model, color) {this.model = model;this.color = color;}rent() {console.log(`${this.model} has been rented`);}returnCar() {console.log(`${this.model} has been returned`);}}const car1 = new Car("Toyota", "Red");car1.rent(); // Toyota has been rentedcar1.returnCar(); // Toyota has been returned
Learning OOP is not just about writing code—it’s about adopting a mindset for structuring software.
✓ Large projects become manageable
✓ Code reusability increases
✓ Data remains secure
✓ Systems stay flexible and maintainable
❋ If you want to build scalable projects, learning OOP is essential!