JavaScript Abstraction
What is Abstraction in JavaScript?
In JavaScript, abstraction refers to the concept of hiding complex details and showing only an object's essential features or functionalities. Simply, it helps us reduce complexity and allows us to design efficiently and implement complex software systems. We can achieve abstraction with either abstract classes or with the help of interfaces An abstraction is a way of hiding the implementation details and showing only the functionality to the users. In other words, it ignores the irrelevant details and shows only the required ones.
Syntax
class AbstractClass {
constructor() {
if (new.target === AbstractClass) {
throw new Error("Cannot instantiate an abstract class.");
}
}
abstractMethod() {
throw new Error("Abstract method must be implemented in the derived class.");
}
}
class ConcreteClass extends AbstractClass {
abstractMethod() {
return "Abstract method implemented in ConcreteClass.";
}
}
const instance = new ConcreteClass();
console.log(instance.abstractMethod());
Output
Abstract method implemented in ConcreteClass.
Key Features of Abstraction:
- Focuses on exposing only necessary functionality while hiding the internal details.
- Abstract classes cannot be instantiated directly.
- Derived classes must implement abstract methods.
Example: Implementing Abstraction
Example:
class Shape {
constructor() {
if (new.target === Shape) {
throw new Error("Cannot instantiate an abstract class.");
}
}
calculateArea() {
throw new Error("Abstract method must be implemented in the derived class.");
}
}
class Rectangle extends Shape {
constructor(width, height) {
super();
this.width = width;
this.height = height;
}
calculateArea() {
return this.width * this.height;
}
}
class Circle extends Shape {
constructor(radius) {
super();
this.radius = radius;
}
calculateArea() {
return Math.PI * this.radius ** 2;
}
}
// Using the classes
const shapes = [
new Rectangle(5, 10),
new Circle(7)
];
shapes.forEach(shape => console.log(shape.calculateArea()));
Output
50
153.93804002589985
153.93804002589985
Explanation of Code:
- The Shape class is treated as an abstract class, preventing direct instantiation.
- Derived classes like Rectangle and Circle implement the calculateArea() method to provide specific behavior.
- This ensures only meaningful instances (e.g., rectangles or circles) are created, hiding unnecessary details.
Advantages of Abstraction:
- Enhances security by exposing only the essential details.
- Reduces complexity by focusing on high-level operations.
- Improves code readability and reusability.
Why do we use Abstraction in JavaScript?
Abstraction in JavaScript, we have several important purposes to use it. Such as:
- Simplification With the use of abstraction, developers can simplify complex systems by hiding unnecessary details and exposing only the essential parts. It makes the code easier to understand and maintain.
- Encapsulation With the use of abstraction, we can encapsulate the implementation details of a particular function and we can allow the developers to focus on the functionality usage rather than how we can implement it. Because of this we can promote modularity and reduce coupling between the different parts of the codebase.