Object Oriented Programming in JavaScript(ES6)
What is OOP?
Object Oriented Programming (OOP) is a programming paradigm that relies on classes and objects. Classes are like blueprints and objects are like instances of a class.
Some topics we should know before moving on to details.
Class: Classes are blueprints from which we can create/instantiate objects. Classes can have attributes and methods.
Object: Objects are instance of a class, that behaves like variables.
Constructor: Constructor is a method of a class, which is invoked at the time of object creation. It initializes the state of objects.
Destructor: Destructors are automatically invoked at the end of the lifetime of an object. Destructors are usually used to deallocate memory and do cleanup for an object when an object destroys. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.
Basic Principles of OOP?
Basically, there are 4 main principles of OOP. Abstraction, inheritance, encapsulation, Polymorphism. Here, we will discuss briefly these topics with examples in TypeScript.
Encapsulation: Encapsulation is wrapping up the data into a single unit. It reduces complexity and increases reusability.
Abstraction: An abstraction is a way of hiding the implementation details and showing only the functionality to the users. Reduce complexity from users.
Here, we create a method a method called createVehicle which contains all functionalities of creating a vehicle. After creating an object with this class we can just create a vehicle with the createVehicle method, we don’t need to know the functionality of this method. This is abstraction actually.
Inheritance: Inheritance is the procedure in which one class can inherit the attributes and methods of another class. It helps to eliminate redundant code.
In this example, Person is our parent class. Another class Student inherits from Person, which means the Student class can access attributes and methods from the Person class. That’s why we can access the great method at student1 obj which is instantiated from Student.
Polymorphism: The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
A real-life example of polymorphism: A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, and an employee. So the same person possesses different behavior in different situations. This is called polymorphism. It helps to refactor ugly switch/case statements.
There are two types of polymorphism.
1. Method Overriding(Runtime polymorphism)
2. Method Overloading(Compile-time polymorphism)
Method Overriding: Declaring a method in a sub-class that is already present in the parent class is known as method overriding. In the method overriding the data types of arguments, no of arguments and sequence must be exactly the same as the parent method. It is also known as runtime polymorphism. (Late Binding)
Method Overloading: Method overloading is a feature that allows a class to have multiple methods with the same name but their argument list must be different. It is also known as compile-time polymorphism. (Early Binding)
What is Static Method?
The static method of a class is only bound to the class, so we can not use this with an instance of a class but can use this directly with the class.
Access Modifier: Access modifiers in a class are used to set up accessibility (Visibility) for the class members.
There are three types of access modifiers:
Public: All the class members declared under the public modifier will be available to everyone.
Private: The class members declared as private can be accessed only by the member functions inside the class.
Protected: The protected modifiers are also similar to private but they can also be accessed from their own class and any sub-class of that class.
Singleton Design Pattern Implementation using TypeScript
Singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance.
Welcome to read this article, and any comments & suggestions are welcome.
By the way, this is my first article on Medium.