← All Articles

Understanding Object Oriented Programming, the simpler way!

Posted on

Object oriented programming (OOP) sounded something scary during my college days. I believe this might have been the same for some of you and some of you are still having that feeling. I remember, till the last date of my college, I understood the object as, an instance of the class and a real-world entity, without knowing what exactly it means. And the definition of class was like

A class is a user defined data-type which has data members and member functions.

funny

Better get Object oriented programming(OOP) concept clear

I understood these concepts only after starting the job where I actually implemented them. I felt much better when I understood the OOP concept in the right way.

Today, I am here to try helping you understand those concepts with some real life analogies. Believe me writing the code will not be that hard when we properly understand them.

Object Oriented programming is just an approach of writing code for better maintainability and reusability. Stick to the rules, makes complex code easier to develop, reuse and maintain.

Lets start with one analogy

Someone in the late 90’s wanted to have something portable through which he can call his loved ones whenever he is far away, take a picture when he visits a beautiful place, capture the moments with friend and store then for days and days. Watch movies and play games whenever he feels bored. Moreover, keep that thing alive so that he can access it whenever he likes.

The requirements seems familiar, right? Of course, he was thinking about the smartphone. So, he gathered all these components:

Screen, Battery, Camera, Charging Port, Storage space, an all other hardware components.

With lots of research and hard work he manufactured his dream device which will allow him to do:

Watch movies

Call Someone

Take pictures

Play games

Charge phone

Store photos and videos

And many more

Yes i did it!

Now he has an idea or we can say blueprint to create the smartphones for his family and friends. He can even manufacture millions of copies.

Let’s utilize this scenario to learn class and object.

The blueprint that he designed to create smartphones can be considered as class. Lets name that class as Smartphone since we will be using it to create lots of smartphones.

Let’s bring the definition of class that we saw earlier

A class is a user defined data-type which has data members and member functions.

Now it seems somewhat familiar. But lets decompose it more. Its a user-defined data-type - We saw from the scenario, the man created something that was completely as he defined and wanted it to be like. Neither we can represent that device he made as integer nor string nor array but we can tell it as smartphone type and it will sound perfect.

It has data members or we can call them as attributes. These are the property that will describe our smartphone class. Screen, battery, storage space, charging port all are the attributes.

Attributes describe how smartphones look, but it does not tell how it works or what it is capable of doing. For this, it does have member functions or we can call them as methods. As we saw earlier, the smartphone will be used to make call, capture picture, make videos, etc. All of these are member functions.

We also saw that class can also be defined as a blueprint using which we can make many objects.

Wait Wait Wait

What is an object?

Before explaining that, let me add one more thing with the analogy. We considered that the man who manufactured will be making million of smartphones. But does all need to look same or should have the same features or quality. Obviously, we will make differences on them based on the cost so that he could target both rich and poor people.

An object is something that has been manufactured. The first copy that he made from him using the smartphone class was also the object. And now he has created millions of phones and all those are objects too. Although, all those object might not look similar or behave in similar way but they all are smartphones. They differ by the value of attributes they are built on.

For example, one phone might have large screen with higher megapixel camera available on higher price and other can have smaller screen with lower camera quality, However both have the qualities to consider them as smartphone.

Let’s bring the definition.

object is an instance of class.

Yes, its straightforward. It is the real entity that we can see and it brings our concept of smartphone class to life.

point

Before wrapping up, Let’s try to manufacture our own smartphone. My smartphone will be like:

Screen = 6 inch

Battery = 4100 mh

Memory space = 16gb

camera = 48 megapixels

charging port = fast charging type c port

Based on these attributes, it will allow me to perform different operations like watch movies, take good pictures, capture videos and so on.

You might try to create your own smartphone. Obviously, you can have it as you like.

Want to know more about Object oriented programming (OOP)?

programmingobject-oriented-conceptclassobjectoop