Introduction to Object-Oriented Programming (OOP)
It is often said that the best way to test someone’s knowledge is by asking them to explain the concept to a 6-year-old. Definitely not a small feat but if you do it right, well then you have the respect of the entire room.
So the topic for today is “Introduction to Object-Oriented Programming” and can you guess what I am going to do?
Well yes, you thought it right! I am going to explain it as I would to a 6-year-old.
So, let’s suppose we have a Tesla. What is a Tesla? It’s a car. A car enables you to go from one place to other with ease. Now a really simple question- Does one really need to know the inner mechanics of a car to actually drive it?
The answer is no. One only needs to know how to drive a car and voila! They can use it to travel just by having a driver’s license and by knowing how to drive. This clearly depicts that the background details can be hidden so as to focus on details of greater importance. That is “Abstraction” for you.
In the case of self-driving Tesla car models, you don’t even know how to drive a car because the car will navigate on its own. That’s an entirely new level of abstraction altogether!
By definition, abstraction is the concept of object-oriented programming that “shows” only essential attributes and “hides” the unnecessary information. It is one of the four cornerstones of Object-Oriented Programming.
Now let’s talk about “Encapsulation”, the second cornerstone of OOP. Now imagine you are driving the Tesla down the road. You place your foot on the gas pedal when you want to accelerate and on the brake pedal when you want to bring your car to a stop. This causes the internal state of the Tesla to change. But it definitely will not affect the cars around you. Thus, clearly, the internal state of the Tesla is encapsulated.
By definition, encapsulation is the concept of bundling of data and methods that work as a unit. It also helps in hiding the internal representation of an object from other objects.
Till now, we were only talking about a Tesla. We could even have a Ferrari, a Honda, a Lamborghini. They could be of different colors, different models and different specifications. But at the end of the day, they are all just one thing- a car. A car here is nothing but an object. Hence, we can say a car can be available in different forms. That is what “Polymorphism” is.
The word polymorphism simply means having many forms. In OOP, polymorphism is the ability of an object to take different forms. It can also be used to define a method that performs a single action in different ways.
Coming to our last fundamental OOP concept, think about all the other ways you can travel. You can travel by driving a truck or by riding a scooter or a motorbike. What is common to all of these? They’re all vehicles- some two-wheelers and some four-wheelers.
Now think about “Inheritance”. In English, it simply means inheriting or getting something from someone. In OOP, inheritance refers to the mechanism in which one class acquires the property of another class, just like a child inherits from their parents.
Circling back to thinking about all the other ways you can travel- A car is obviously a four-wheeler vehicle and thus inherits all the properties of being a vehicle. Hence, inheritance is at play here where “vehicle” is a parent class and “car” is the child class.
To conclude, now you know all about the four cornerstones of Object-Oriented Programming.
Thank you for taking the time to read this!