Loading

Quipoin Menu

Learn • Practice • Grow

java-script / Abstraction
interview

Q1. What is abstraction?
Abstraction is the process of hiding complex implementation details and showing only the essential features of an object.
Abstraction helps users interact with objects without knowing how they work internally. It reduces complexity and improves code readability.

Example
When you use a car, you only use the steering wheel, brakes, and accelerator. You do not need to understand how the engine works internally. Similarly, abstraction hides internal logic and exposes only necessary functionality.


Q2. Why is Abstraction important in JavaScript?
Abstraction helps simplify complex systems and improves code maintainability.
It allows developers to focus on what an object does instead of how it does it. This makes applications easier to manage and scale.


Q3. How can Abstraction be implemented in JavaScript?
Abstraction can be implemented using classes, interfaces (conceptually), and access control like private methods and properties.

Example
class Car {
  startEngine() {
    return "Engine Started";
  }

  drive() {
    return this.startEngine() + " and Car is moving";
  }
}

const car = new Car();
console.log(car.drive());

The user only calls drive() and does not need to know how startEngine() works internally.


Q4. What are abstract classes in JavaScript?
JavaScript does not have built-in abstract classes, but they can be simulated using base classes that are not meant to be directly instantiated.

Example
class Shape {
  constructor() {
    if (this.constructor === Shape) {
      throw new Error("Abstract class cannot be instantiated");
    }
  }

  area() {
    throw new Error("Method must be implemented");
  }
}


Q5. Can Abstraction be achieved using functions?
Yes, abstraction can be achieved using functions that hide internal logic and expose only necessary functionality.

Example
function createBankAccount(balance) {
  function validate(amount) {
    return amount > 0;
  }

  return {
    deposit(amount) {
      if (validate(amount)) {
        balance += amount;
      }
    },
    getBalance() {
      return balance;
    }
  };
}

const account = createBankAccount(1000);
account.deposit(500);
console.log(account.getBalance());