Abstraction

Mansi Shrivastava
2 min readOct 14, 2023

--

in object-oriented programming (OOP) in a simple way.

Abstraction is one of the fundamental concepts of OOP, and it’s all about simplifying complex systems by focusing on the essential features while hiding the unnecessary details.

// Define a class representing a Car
class Car {
constructor(make, model, color) {
this.make = make;
this.model = model;
this.color = color;
this.speed = 0;
}

// Method to accelerate
accelerate() {
this.speed += 10;
}

// Method to brake
brake() {
this.speed -= 5;
}

// Method to get car info
getInfo() {
return `A ${this.color} ${this.make} ${this.model} is traveling at ${this.speed} mph.`;
}
}

// Create an instance of a car
const myCar = new Car("Toyota", "Camry", "Blue");

// Use the car's methods to interact with it
myCar.accelerate();
myCar.accelerate();
myCar.brake();

// Get information about the car
console.log(myCar.getInfo());

let’s break down the importance of abstraction into simpler terms:

  1. Simplifying Complexity: Abstraction simplifies things by focusing on what’s important and hiding the tricky stuff. It’s like enjoying a pizza without needing to know how it was baked.
  2. Hiding Complex Stuff: Abstraction keeps the complex inner workings out of sight. It’s like driving a car without needing to understand the engine.
  3. Reuse and Recycle: Abstraction lets you reuse what you’ve created. It’s like using the same recipe for different cakes, just changing the flavors.
  4. Taking Things Apart: Abstraction divides big problems into smaller, manageable pieces. It’s like building a LEGO set, one piece at a time.
  5. Easy Upkeep: Abstraction makes it simpler to take care of your code. It’s like maintaining a garden by trimming individual plants instead of the entire jungle.
  6. Teamwork: Abstraction helps teams work together. It’s like building a puzzle — everyone focuses on their piece, but they fit together to create a complete picture.
  7. Clear Ideas: Abstraction makes your code easier to understand. It’s like telling a story with pictures instead of words; it’s clearer and more engaging.
  8. Ready for Change: Abstraction makes your code ready for anything. It’s like having clothes for all seasons — you can adapt without starting from scratch.

--

--

Mansi Shrivastava
Mansi Shrivastava

Written by Mansi Shrivastava

I specialise in building user-friendly and visually appealing websites and applications.

No responses yet