Javascript Abstraction
Abstraction is a very important concept in OOPS programming. Since we argue that JavaScript also follows OOP concepts, then it should be possible to create the abstract classes in JavaScript also.
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 one.
- We cannot create an instance of Abstract Class.
- It reduces the duplication of code.
Syntax:
abstract class className(){
}
Example:Abstract
abstract class Animal{
name:string=” ”’
abstract sound():void;
}
The process of abstraction has two main components:
Generalization is the process of finding similarities in repeated patterns, and hiding the similarities behind an abstraction.
Specialization is the process of using the abstraction, supplying only what is different (the meaningful) for each use case.
Functions make great abstractions because they possess the qualities that are essential for a good abstraction:
Identity — The ability to assign a name to it and reuse it in different contexts.
Composition — The ability to compose simple functions to form more complex functions.
Note: You can achieve 0–100% abstraction using an abstract class.
Properties:
- You can not create instance of an abstract class.
- Abstract class is a half defined parent class.
- Abstract class may contain both abstract and non abstract methods.
- A proper abstract class should have some implementations and some empty declarations.
Suppose someone has written abstract class like this:
abstract class Data{
abstract Add:void();
}
So, now this has become an interface because there is nothing like half defined in it, method is also not defined. And making such kind of class worth nothing, It is meaningless.