DEV Community

Akshat Sharma
Akshat Sharma

Posted on

Day 13of 30 of JavaScript

Hey reader👋 Hope you are doing well😊
In the last post we have completed arrays, it's properties and methods. In this post we are going to starts Object Oriented Programming through JavaScript, we are going to start from very basic and take it to advanced level.
So let's get started🔥

What is Object Oreinted?

Object Oriented Programming is the way of defining complex structures in a simpler way using objects and classes.
Let's understand this with the help of an example-:
Suppose you have a car and this car has a name, type(sports, luxury, passenger etc), number of seats ,engine capacity and many more. Now as you can see that every property described here are all belong to car so it's better to have an entity that can store every property and method(here task that car can perform) at one place, and to do this we use object oriented programming. We will define a car class which is going to contain all the properties and methods related to car and will utilize this class using a car object.
Image description
So here you can see that we have made a car class and defined all the properties and methods in this and accessed them using object.
Don't bother much about constructor and this we will talk about them in detail in coming blogs.

Object Oriented programming is all about the use of objects and classes.

Fundamental blocks of Object Oriented Programming

1. Classes

Classes are entities that holds every related property and method at single place. In our example above we have a car class that contains every property and method related to car.

2. Objects

An object is an instance of class. Using an object we can easily access the property and methods defined in that particular class.

Note-: Class doesn't get any space in memory till its object is defined. We can also say that an object is reference to class.
A class is a blueprint for objects.
In our above example audi was our car class object.

3. Properties or Attributes

These are the characteristics of the class. In our car class name and type were the characteristics of the car class.

4. Methods

These are the actions that objects can perform. In the car class, methods might be drive, stop, and honk. In above example we had get_info method.

Properties of Object Oriented

The key properties of object-oriented programming (OOP) are:

1. Encapsulation

Encapsulation refers to storing everything at one place. In classes we define every related property and method at one place. You can understand it as a capsule that contains medicine in it and the capsule protects the medicine inside it from any external germ.

Bundling data (attributes) and methods (functions) that operate on the data into a single unit or class, and restricting direct access to some of the object's components.

2. Abstraction

Abstraction refers to hiding the complex implementation details and showing or exposing only necessary features. For example, when you apply brakes of vehicle you are not aware of what forces are working in background on application of brakes, all you know is applying brakes will slow down your vehicle, so here the background tasks are abstracted from you.

Hiding the complex implementation details and showing only the necessary features of an object. This helps in reducing complexity and allows the programmer to focus on interactions at a higher level.

3. Inheritance

Inheritance can be understood as inheriting the features of a class by another class. The class whose properties and methods are inherited is called parent class or super class and the class which inherits is called child class or sub class.
Suppose you have a Vehicle class and a Car class. So this Car class will inherit the propeties and methods of Vehicle class.

Creating a new class from an existing class. The new class (child class) inherits attributes and methods from the existing class (parent class), promoting code reuse and establishing a natural hierarchy.

4. Polymorphism

Polymorphism refers to the ability of something to have one form or another according to the requirement. For example your mother plays different roles in different relations; for you she is your mother, for your father she is his wife; for your grandparents she is their daughter in law and many more.
So here also we can have methods that can have different roles for different objects.
Suppose we have a super class called Shape and Square and Circle are our subclasses. We have a calculate_area() defined in our superclass, we can use this method in our subclasses and can implement it according to need. So here calculate_area() has different forms in different classes.

Allowing objects of different classes to be treated as objects of a common superclass. It enables a single interface to be used for a general class of actions, with specific behavior determined by the exact nature of the situation or the object class.

So this is it for this blog. I hope you have understood it well. In the next blog we will go more deep in it. Till then stay connected and don't forget to follow me 🤍

Top comments (0)