DEV Community

Shoyab khan
Shoyab khan

Posted on

Day 13 of 30 of JavaScript

Introduction

Hey reader! 👋 I hope you’re doing well. 😊 In the last post, we completed our discussion on arrays, their properties, and methods. Today, we’re diving into Object-Oriented Programming (OOP) in JavaScript, starting from the basics and progressing to more advanced concepts. Let’s get started! 🔥

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to define complex structures in a simpler way.

Example: Imagine you have a car. This car has various properties such as its name, type (sports, luxury, passenger, etc.), number of seats, engine capacity, and more. Instead of managing these properties individually, OOP allows us to encapsulate them within a single entity—an object. We can define a Car class that contains all the properties and methods related to a car, and then create car objects from this class.

class Car {
    constructor(name, type, seats, engineCapacity) {
        this.name = name;
        this.type = type;
        this.seats = seats;
        this.engineCapacity = engineCapacity;
    }

    getInfo() {
        return `${this.name} is a ${this.type} car with ${this.seats} seats and an engine capacity of ${this.engineCapacity}L.`;
    }
}

const myCar = new Car('Audi', 'Luxury', 5, 3.0);
console.log(myCar.getInfo());
Enter fullscreen mode Exit fullscreen mode

In this example, we define a Car class with properties and methods, and we access them using an object.

Fundamental Blocks of Object-Oriented Programming

  1. Classes:
    Classes are blueprints that hold related properties and methods in one place. In our example, the Car class contains all the relevant details about a car.

  2. Objects:
    An object is an instance of a class. It allows us to access the properties and methods defined in that class. Note that a class does not occupy memory until an object is created from it.

  3. Properties or Attributes:
    These are characteristics of the class. In our Car class, properties like name and type define the characteristics of the car.

  4. Methods:
    Methods are actions that objects can perform. In the Car class, methods might include actions like drive, stop, or honk. In our example, we have the getInfo method.

Properties of Object-Oriented Programming

  1. Encapsulation:
    Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit or class. It restricts direct access to some components, similar to how a capsule protects its contents.

  2. Abstraction:
    Abstraction involves hiding complex implementation details and exposing only the necessary features. For instance, when you apply the brakes in a vehicle, you don’t need to know the intricate mechanics behind it; you just know that applying the brakes slows down the vehicle.

  3. Inheritance:
    Inheritance allows one class to inherit properties and methods from another class. The class being inherited from is called the parent class (or superclass), while the class that inherits is called the child class (or subclass). For example, a Car class can inherit from a Vehicle class.

  4. Polymorphism:
    Polymorphism refers to the ability of different classes to provide a unique implementation of methods that share the same name. For example, a Shape superclass might have a calculateArea() method, which can be implemented differently in subclasses like Square and Circle.

Conclusion

That’s it for today’s introduction to Object-Oriented Programming in JavaScript! I hope you found this information helpful. In the next blog, we’ll delve deeper into OOP concepts. Until then, stay connected, and don’t forget to follow me!

Top comments (0)