Many programmers, especially when starting out, find it hard to really understand Object-Oriented Programming (OOP). OOP principles (encapsulation, abstraction, inheritance, polymorphism) tend to be hurled at you so fast, and yes, it can get intimidating.
Well, it felt that way for me too until I finally got it! And it is a simple concept.
If you are like me, you would enjoy writing and thinking about your code under this concept once you see how related it is to how we live life itself.
The secret no one tells you is *inserts dramatic music Object-Oriented Programming is your superpower.
This article will be the first in a series expounding Object-Oriented Programming and relating it with life as you already know it.
Start your imagination. Let's go!
Orientation of Objects
There are objects all around us. We would conventionally infer that an object is a non-living thing, but not so in this case. Objects can be anything - even you. Everything can be seen as an object.
An Object has some characteristics and may perform or receive some actions that change those characteristics.
Let us take an example.
A drinking cup.
Every cup has characteristics such as: whether the cup is empty or full, its colour, its volume (how much liquid it can hold in a cubic measurement), it's content, what material it is made from, what type of cup it is (a coffee mug, teacup, wine glass, tot glass, beer cup), its weight, and on and on.
A cup can also receive action. You can pour into a cup, or out of it and this directly changes how filled or empty it is, what its content is, etc.
Another example
Let's examine a human being as an object.
Characteristics you have as a human being include height, weight, the colour of your eyes, your skillset, where you live, who you live with, your current emotional state, whether you are happy or not, who your friends are, what part of the earth you are from, and the other things only you or the FBI knows. Lol.
You can perform or receive action that changes any of these characteristics.
You grow, which changes your height and weight. You could relocate to a new country, which changes the address where you live, etc.
You can receive action, such as a friend leaving you, which changes who your friends are.
Photo by Norbert Levajsics on Unsplash
Take a brief look around and mentally select an object. Even if you are alone in the middle of the desert, or reading this article from space, there is at least one object near you. May be your clothes, sand, your spaceship, the big blue ball we agreed to call earth, etc. Mentally pick one.
Can you list some characteristics of this object? Yes? Great.
Can you identify some actions this object can perform or receive to change one or a combination of those characteristics? Yes again?
Wonderful!
If your answers were not yes to these questions, pick another object, or continue reading. Try to repeat this later and it will get some more light in a bit.
What Is Object-Oriented Programming?
The secret no one tells you is Object-Oriented Programming is your superpower.
Yup! That's what it is. That excitement and frustration you may be feeling right now or felt when it was new to you is normal for all new sups. You're fine. 😉
Just like life is an interaction between several objects. You using your cups (filling and drinking out of them), you relocating, changing your friends, wearing clothes, spending money on food, dancing to music, governments changing policies that affect your business, etc., object-oriented programming is programming by writing code as an interaction between several objects - objects of types that either you or other people invented.
Took the liberty to highlight examples of objects in the example above.
OOP gives you the power to create and use whatever kind of object you can imagine in code.
Amazing! Yes, it felt amazing for me, knowing I get to create small worlds that include my type of objects interacting with each other to form a software product 😎.
That is it. That is Object-Oriented Programming - writing your code like objects. Whether it is a networking object you want with characteristics such as the server address you are connecting to, and ability to perform actions like fetch user data. Or a user object for users of your software to hold their details and receive actions like editing or deleting their account. That is the concept of OOP.
In the next part, we will discuss the components of OOP. Classes, properties, constructors and methods.
Leave a reaction and share if you enjoyed this read. Thank you.