OOPs in Javascript

We will start from object oriented programming basics like

  1. What are different concepts in Object Oriented Programming.
  2. Why should we incorporate object oriented programming.
  3. Why can’t we work only with functions in Javascript.
  4. We will discuss the role of object oriented programming in any programming language and what is object oriented programming in general.

So, Object Oriented Programming is made up of 3 words. Object + Oriented + Programming

So, if we have to understand Object Oriented Programming, then we will have to understand Object first.

Object -> 1st Concept

Object – Object means a Real Time Object like Pen, Paper, Bill, Building, Rent Receipt etc…

Every Object is associated with a class, So we have to understand class as well.

Class -> 2nd Concept

Class – class means the layout or the blueprint of an object, like design of the pen, Layout of a Bill, Blueprint of a Building, Format of a Rent Receipt.

Then, we have a constructor associated to every class, so we must understand constructor as well.

Constructor -> 3rd Concept

Constructor – Constructor is a special type of function, which gets executed as soon as the object of a class is created.

Then we have to understand a 4th concept, Types of Object

Types of Objects -> 4th Concept

Methods -> 5th Concept

Methods – Methods are same like a function, but a method is always associated with a class or an object, there can be no method without a class or object. function is never associated with any class.

Differnce between function and method -> 6th Concept

Then There are 4 pillars of Object Oriented Programming

  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

Let’s understand these concepts first.

Inheritance – Let’s take an example of a Car of Suzuki like Celerio. If you search for Celerio online, then you will find that there are different variants available of Celerio like lxi, vxi, zxi and zxi+. The lxi variant is the base model, and all the other variants vxi, zxi and zxi+ are made using the lxi base model, Suzuki didn’t made all the variants from scratch, They have just made a base model and used the base model to make other variants where some extra features are added. So, Inheritance is the way that genetic information is passed from a parent to a child. So, here base model is parent and all the other variants are child.

There are 2 types of Inheritance in Javascript

Prototypes and Prototypal Inheritance -> 7th Concept

Inheritance using extends keyword & Parent Child Concept -> 8th Concept

Polymorphism – Polymorphism meaning is many form, So, anything that is found in many forms, is said to have a property of Polymorphism.
Let’s take an example of Laptops, We see that the laptops in the market come with different configurations like We get

  • Some Laptops with Windows OS and Intel Processor
  • Some Laptops with Windows OS and AMD Processor
  • Some Laptops with i3 intel processor
  • Some Laptops with i5 intel processor
  • Some Laptops with i7 intel processor
  • Some Laptops with MacOS
  • Some Laptops with Linux OS

So, Laptops are coming in different forms, This is called Polymorphism. So, we have following concepts in polymorphism

function overloading -> 9th Concept

function overriding -> 10th Concept

method overloading -> 11th Concept

method overriding -> 12th Concept

Constructor Overloading -> 13th Concept

Constructor Overriding -> 14th Concept

Then we have concept of Abstraction and Encapsulation

Abstraction -> 15th Concept

Abstraction – Hiding internal details or Hiding implementation or Hiding methods. Let’s take an example of a Car. When we drive a car, then the speed of Car will increase when we press accelrator, and the speed of the car will reduce when we press break. Now, we don’t know about the internal working of accelarator and break, so the internal details or the implementation of accelarator and break is hidden from us. This is called Abstraction.

Encapsulation -> 16th Concept

Encapsulation – If we have to create a laptop, then we need different different things like, Speaker, mother board, hard disc, RAM etc.. and all these different different things make a single entity laptop, but all these things like, Speaker, mother board, hard disc, RAM etc.. are combined together to form a single unit i.e. laptop. So, all these things like Speaker, mother board, hard disk etc. are encapsulated. This is the concept of Encapsulation.

So, Let’s take a look at all the concepts, that we are going to discuss as part of Object Oriented Programming in Javascript.

  1. Objects
  2. Classes
  3. Constructor
  4. Types of Objects
  5. Methods
  6. Difference between function and Method
  7. Inheritance
    • Prototypal Inheritance
    • Inheritance using extends keyword and parent child concept
  8. Polymorphism
    • function overloading
    • function overriding
    • method overloading
    • method overriding
    • constructor overloading
    • constructor overriding
  9. Abstraction
  10. Encapsulation

What is the importance of Object Oriented Programming

Before we understand object oriented programming, We must understand, why we do programming, and the answer to that question is, we do programming to make our life easy in real world.

  1. You can take an example of Swiggy, Zomato, these are online food delivery applications and they have made the life easy for those who want to order their favorite food from their favorite restaurant.
  2. You can take an example of our mobile phone, we can do multiple tasks from our mobile phone, like calling, taking photos, taking videos, use it as a clock, we can watch movie and what not

So you take an example of Swiggy, Zomato, or a Camera in your mobile, or a clock in a mobile, all these things are software and softwares are created by programming. So, Programming makes our life easy. Same way, Object Oriented Programming makes programming easy. So, if some one asks us that why do we need object oriented programming, Can’t we do programming using functional programming only, then the answer is, We can work only with functional programming as well but Object Oriented Programming makes programming easy.

Now, one thing to note is that, We don’t use Object Oriented Programming in Javascript much, but we should know about Object Oriented Programming.

Leave a Comment