Back to basis: Object-oriented programming in 3 mins

Manan Kumar Gupta
3 min readMay 2, 2021

Object-oriented programming is one the most popular programming paradigm which almost every developer must have encountered and it is asked in the interviews very often.

Before starting let's understand what is meaning of Paradigm?

It simply means the pattern of looking or implementing something. So the way or methodology of implementing our program is called the programming paradigm. There are mainly 2 types of Programming paradigm:

  1. Object-Oriented Programming
  2. Functional Programming

What is the OOP paradigm?

If you are a beginner in the world of programming you would be probably defining various functions to perform different tasks and then call them from different places in your code, This paradigm is called Functional programming.

Object-oriented Programming more like real-world entities and is easy to relate with real-world scenarios.

Class Vs Object:

Class is the skeleton or structure of an entity. Like all vehicles in the world have a common property like speed, normalSpeed, and weight.
Where an Object is an entity based on the Class. So all the cars in this world would an object of class “Vehicle”.

// JavaScript implementation of class
// Defining class Vehicle
class Vehicle {
// constructor function is called automatically on initialisation
constructor(weight,normalSpeed){
this.weight = weight;
this.normalSpeed = normalSpeed;
}
start(){
console.log("Start function is called");
}
}
// Intialising object based on Vehicle
let car1 = new Vehicle(100,35); //Declaration and Initialisation of variable
car1.weight = 100;
car1.normalSpeed = 25;
car1.start()

Key features of OOP that we will learn are:

  1. Encapsulation:
  2. Inheritance
  3. Abstraction
  4. Polym

Encapsulation:

As the name suggests the wrapping of properties or processes for a class. By doing this we make sure that those properties or functions are meant for the object of that class.

In the above code, we have declared that 2 variables for the class vehicle which defines that all the variables based on the class vehicle will have these 2 properties.

Inheritance:

In this world of vehicles, there are many types of vehicles like bicycles and cars, and vans, All of these cannot be said to be an object of the “Vehicle” but they are the subdivision of vehicle so here comes the role of Inheritance in this we define other class which is derived from superclass or base class.

// Car is another class which is deried from Vehicleclass Car extends Vehicle {
constructor(weight,normalSpeed, fueltype) {
// This calls the constructor function of parent class.
super(weight,normalSpeed);
this.fueltype = fueltype;
}
show() {
console.log("Show function from Car class");
}
}
let car1 = new Car(100,35,"Petrol");
console.log(car1.weight) // 100
car1.start() // Parent Class function are accessible

Abstraction

An abstraction is a method 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.]

I know this didn’t make much sense but it is one of the key features of OOP. Assume you want to prevent the user from making the object directly from the class Vehicle here Abstract will have.

class Vehicle {
constructor(weight,normalSpeed){
if (this.constructor == Vehicle) {
throw new Error("Abstract classes can't be instantiated.");
}
this.weight = weight;
this.normalSpeed = normalSpeed;
}
start(){
throw new Error("Abstract method!");
}
}

In the above, we made sure that class Vehicle is a model for other classes it cannot be used to created objects directly.

I hope you have found this useful. Thank you for reading.

If you want to read more refer to this link.

--

--

Manan Kumar Gupta

I am Frontend Developer, exploring the power of Javascript. I have worked on Angular, ReactJS and React Native.