[Go to site: main page, start]

0% found this document useful (0 votes)
10 views3 pages

JavaScript Abstraction and Abstract Classes

Abstraction in object-oriented programming (OOP) involves hiding implementation details and exposing essential features, which can be simulated in JavaScript using function constructors and prototypes. Abstract classes cannot be instantiated and contain abstract methods that must be implemented by derived classes, as shown in the example with the Shape and Circle classes. Additionally, interfaces, while not natively supported in JavaScript, can be implemented using libraries like TypeScript to define required methods for classes.

Uploaded by

kzia9947
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

JavaScript Abstraction and Abstract Classes

Abstraction in object-oriented programming (OOP) involves hiding implementation details and exposing essential features, which can be simulated in JavaScript using function constructors and prototypes. Abstract classes cannot be instantiated and contain abstract methods that must be implemented by derived classes, as shown in the example with the Shape and Circle classes. Additionally, interfaces, while not natively supported in JavaScript, can be implemented using libraries like TypeScript to define required methods for classes.

Uploaded by

kzia9947
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

</>CodeWithHarry

Start Learning Tutorials

JavaScript Tutorial OOPs

Abstraction

Abstraction is a fundamental concept in object-oriented programming (OOP) that refers to the


practice of hiding the implementation details of an object and exposing only the essential features
to the user. In JavaScript, abstraction is achieved by using abstract classes and interfaces.

An abstract class is a class that cannot be instantiated and is meant to be used as a base class for
other classes. Abstract classes typically contain one or more abstract methods, which are methods
that have a signature but no implementation. These methods must be implemented by the derived
classes.

JavaScript does not support abstract classes natively, but you can achieve similar functionality by
using a combination of function constructors and prototypes.

For example, consider the following code:

function Shape() {
if ([Link] === Shape) {
throw new Error("Cannot instantiate abstract class Shape");
}
[Link] = function() {
throw new Error("Cannot call abstract method draw from Shape");
}
}

function Circle() {
[Link](this);
[Link] = function() {
[Link]("Drawing a Circle");
}
}
[Link] = [Link]([Link]);
[Link] = Circle;

let circle = new Circle();


[Link](); // "Drawing a Circle"
let shape = new Shape(); // Error: Cannot instantiate abstract class Shape

In this example, the Shape class is an abstract class that cannot be instantiated and contains an
abstract method draw() which throws an error when called. The Circle class inherits from the
Shape class and implements the draw() method providing a specific implementation.

Interfaces are another way to achieve abstraction in JavaScript, although it's not a native feature of
JavaScript. Some libraries like TypeScript provide interfaces. An interface defines a set of methods
that a class must implement but does not provide an implementation for those methods. This
allows developers to ensure that a class has the required methods without specifying how those
methods should be implemented.

Previous: Polymorphism

Main Learn

Home Courses

Contact Tutorials

Work With Us Notes

My Gear

Legal Social

Terms GitHub

Privacy Twitter (X)

Refund YouTube

Facebook
Made with ❤️ ☕
and in India

You might also like