DEV Community

Ramesh0807
Ramesh0807

Posted on

Let's explore OOP concepts using the example of a Royal Enfield motorcycle

Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of organizing data and code into objects, which are instances of classes. OOP encourages the use of classes and objects to model real-world entities and their interactions

Class: A class is a blueprint for creating objects. In the context of a Royal Enfield bike, we can define a Bike class.

class Bike {
  constructor(model, color) {
    this.model = model;
    this.color = color;
    this.speed = 0;
  }

  start() {
    console.log(`${this.color} ${this.model} bike is started.`);
  }

  accelerate(speed) {
    this.speed += speed;
    console.log(`Accelerating to ${this.speed} km/h.`);
  }

  brake() {
    this.speed = 0;
    console.log("Brakes applied. Bike is stopped.");
  }
}
Enter fullscreen mode Exit fullscreen mode

Object: An object is an instance of a class. Here, we can create two instances of the Bike class.

const classic350 = new Bike("Classic 350", "Black");
const bullet500 = new Bike("Bullet 500", "Desert Storm");

Enter fullscreen mode Exit fullscreen mode

Encapsulation: Encapsulation is the concept of bundling data (attributes) and methods (functions) that operate on that data within a single unit, i.e., the class. In our example, the Bike class encapsulates the bike's attributes (model, color, speed) and methods (start, accelerate, brake).

Inheritance: Inheritance allows a class (subclass or derived class) to inherit the properties and behaviors of another class (superclass or base class). For example, we can create a RoyalEnfield subclass that inherits from the Bike class and adds specific features.

class RoyalEnfield extends Bike {
  constructor(model, color, variant) {
    super(model, color);
    this.variant = variant;
  }

  displayDetails() {
    console.log(`Model: ${this.model}, Color: ${this.color}, Variant: ${this.variant}`);
  }
}

Enter fullscreen mode Exit fullscreen mode

Polymorphism: Polymorphism allows objects of different classes to be treated as objects of a common super class. It allows methods with the same name to have different implementations in different classes. In our example, the Bike and RoyalEnfield classes can both have an accelerate method, but they might behave differently.

classic350.start();        // Output: Black Classic 350 bike is started.
classic350.accelerate(60); // Output: Accelerating to 60 km/h.

bullet500.start();         // Output: Desert Storm Bullet 500 bike is started.
bullet500.accelerate(70);  // Output: Accelerating to 70 km/h.

const thunderbirdX = new RoyalEnfield("Thunderbird X", "Blue", "X Series");
thunderbirdX.start();      // Output: Blue Thunderbird X bike is started.
thunderbirdX.displayDetails(); // Output: Model: Thunderbird X, Color: Blue, Variant: X Series

Enter fullscreen mode Exit fullscreen mode

These are some of the fundamental OOP concepts demonstrated through the example of a Royal Enfield bike. OOP provides a structured and organized way to model and interact with real-world entities in a software application.

Top comments (0)