DEV Community

Cover image for Object-Oriented Programming (Easy Description)
Prakash Bhattarai
Prakash Bhattarai

Posted on

Object-Oriented Programming (Easy Description)

Object-Oriented programming is the style of programming where everything is considered as an object. Consider the simple blog application. In this application, we have different things to manage. For example, database, post, user, etc. They have their own properties and behaviors. In the case of a user, every user has properties like username, email, profile, written posts, etc., and also behaviors like logging in, logging out, changing profile image, etc. In OOP, we put all those properties and behaviors (methods) in a single place.
Alt Text

Up to this point, we learned about objects. But it is very difficult to manage all the objects like this and it is also not memory efficient. It would be better if we had some blueprint to create an object. The class comes to the rescue. It is the blueprint to create objects.
Alt Text
Objects created in this way are way more efficient and easier to create. Now we can simply new up the new objects. As many as we want 😊. This will create the object same as before. Well, not exactly 😊 😊. Don’t take me wrong, JavaScript works a bit differently but don’t worry!
Alt Text

Now, it’s time to talk about the four pillars of object-oriented programming.
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism

Encapsulation: What if I told you, we already learned about encapsulation. Yes, we did. The idea of putting properties and behavior at the same place is encapsulation. It is the act of wrapping data (properties) and codes acting on that data (methods) in a single unit. Think of it as making of the capsule 😊.

Abstraction: Abstraction is the process of hiding certain details and showing only essential information. It is encapsulation plus data hiding. Yes, it is. You first encapsulate and then hide the unnecessary details.
Let’s have a look at how to hide details in JavaScript because it is a bit tricky. We can use Symbol and WeakMap to implement data hiding. (It’s easy in programming languages like Java. We can use private or protected keywords to hide properties and methods from outside.)
Alt Text

In this way, we can hide all the unnecessary details and only show the absolutely necessary details. A real-world example of abstraction is remote control. We don’t want users to know about the internal details of the remote. Instead, we provide several buttons which are necessary to perform actions like changing volume, channel, etc.

Inheritance: Before starting, I want to remind you what OOP is all about. It’s about objects, right? Saying that, isn’t it possible that real-world objects have the same ancestor and they share some common behaviors and properties. Dog and human beings are different objects but they share common properties and behaviors of mammals. This is the basic idea of inheritance.
Alt Text

Polymorphism: Ok, up to this point we knew what inheritance is. Now from the above example, we know that both humans and dog feed their babies with milk. But don’t you think there is a difference in how human beings and dogs feed babies. This is polymorphism.
Alt Text

This article focuses on the basic description of object-oriented programming rather than the complete OOP features of any particular programming language. After having a basic idea, you can learn about OOP in some specific languages.
It’s my first article and I am learning OOP and principles of object-oriented designs. If you are more experienced than me and found something wrong please help me by pointing out my mistakes.
Thank You!

Top comments (0)