OOP

Organize code with classes, objects, methods, and inheritance.

As JavaScript applications become larger and more complex, organizing code becomes increasingly important. Writing all code in one place can make applications difficult to maintain and update. Object-Oriented Programming (OOP) is a programming approach that helps developers organize code into reusable and manageable structures.

OOP is based on the idea of creating objects that represent real-world things. These objects contain both data and actions, making programs easier to understand and maintain.

Many modern applications, including e-commerce websites, social media platforms, and banking systems, use OOP concepts extensively.

What Is Object-Oriented Programming?

Object-Oriented Programming is a way of building software using objects and classes.

Think about a smartphone. A smartphone has:

  • Properties such as brand, model, and storage.
  • Actions such as calling, texting, and taking photos.

In JavaScript, we can represent these properties and actions using objects. OOP allows us to model real-world entities in a structured way.

What Is a Class?

A class is like a blueprint for creating objects.

Just as a building blueprint can be used to construct multiple houses, a class can be used to create multiple objects with similar properties and behaviors.

javascript
class User {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
}

The class defines what information each user object should contain.

Creating Objects

Objects are created from a class using the new keyword.

javascript
const user1 = new User("John", 25);

console.log(user1.name);

Output:

javascript
John

Here, user1 is an object created from the User class.

Methods in Classes

A method is a function that belongs to an object.

javascript
class User {
    constructor(name) {
        this.name = name;
    }

    greet() {
        console.log(`Hello, ${this.name}`);
    }
}

const user = new User("Sarah");

user.greet();

Output:

javascript
Hello, Sarah

Methods allow objects to perform actions.

Inheritance

Inheritance allows one class to use properties and methods from another class.

javascript
class Animal {
    speak() {
        console.log("Animal makes a sound");
    }
}

class Dog extends Animal {}

const dog = new Dog();

dog.speak();

Output:

javascript
Animal makes a sound

The Dog class inherits the speak() method from the Animal class.

Encapsulation

Encapsulation means keeping related data and functionality together while protecting data from unnecessary access.

For example, a banking application may hide account balance calculations from users while still allowing them to view their balance. This improves security and code organization.

Polymorphism

Polymorphism allows different objects to use the same method name but produce different results.

javascript
class Cat {
    speak() {
        console.log("Meow");
    }
}

class Dog {
    speak() {
        console.log("Woof");
    }
}

Both classes use the speak() method, but each produces a different output.

Real-World Example

Imagine an online shopping website.

javascript
class Product {
    constructor(name, price) {
        this.name = name;
        this.price = price;
    }
}

const laptop = new Product("Laptop", 50000);
const phone = new Product("Phone", 25000);

Using a class allows developers to create many products without repeating code.

Benefits of OOP

  • Better code organization
  • Easier maintenance
  • Code reusability
  • Improved scalability
  • Simplified development of large applications

These benefits become more important as projects grow in size.

Summary

Object-Oriented Programming organizes code using classes and objects. It helps developers create reusable, maintainable, and scalable applications. Concepts such as classes, objects, methods, inheritance, encapsulation, and polymorphism make it easier to model real-world situations in code.

Let's learn with DevBrainBox AI