Object-Oriented Programming is just another concept!
Object-Oriented Programming can be a nightmare to learn for many beginners! OOP with C++ is a wonderful head start and hopefully, it is the most interesting and simplest approach to learning OOP!
In simple terms, OOP is a method of programming where you consider programs to be written based on real-world objects. Some of the common examples you would have come across would be Bank Accounts, Humans, Vehicles, and so on.
The difference between OOP and the usual procedural/functional programming that you have been learning all these days is that OOP is inclusive of everything in procedural programming but, OOP consists of classes and objects.
A class is like a set! It’s a blueprint or template that can be used to create objects. Its the mold to your star-shaped cookies! A class consists of attributes and methods. It’s simple!
Attributes are similar to variables that you have learned in functional programming, except that attributes exclusively define the physical aspects of the class, and variables can be anything.
A method is similar to functions that you have learned in functional programming, except that they exclusively define the functionalities of the class.
The four main concepts of OOP are abstraction, encapsulation, polymorphisms, and inheritance, which makes it uniquely outstand other programming paradigms.
Abstraction is the prioritizing of important details. This concept brings about a certain level of protection, where the user will be exposed to only what the user should know, and the additional details are hidden. This is usually achieved by implementing an Interface!
This feature is commonly used in our day-to-day activities:
Suppose when driving a car, you would only interact with the interface (ie. steering wheel, accelerator, and brakes) and not pay attention to how the engine is functioning. It is unnecessary detail to just drive your car.
Encapsulation is the term used to protect data. As we can see now, we are adding the necessary data within a “capsule” so that it cannot be accessed by anyone else.
Polymorphism means “many forms”, as many of you would have heard it. But what does it exactly mean? Polymorphism is the use of functions that produces output based on the input. Taking a real-world example:
Making sound to communicate is a common functionality amongst animals, however, when considering different animals, they make different sounds. Humans speak Duck quacks, Cow moos, Pig grunts, and so on.
If we give our minds the thought, we can come to a clear understanding that they all do the same thing, but based on different inputs, the outputs are different.
Inheritance is simply a child class being able to inherit the attributes of the parent class. However, the child class may have additional attributes and functionalities over the parent class. A good example to simply understand this concept would be our parents and us:
Our parents have had certain attributes that we have inherited. This could be the color of our hair or eyes, maybe certain skills. But in addition to that, we would also have some characteristics of our own. Maybe we are faster runners than our parents? Or maybe we have a better immunity system than they do!
I have been through the hassles of understanding the concepts of OOP. This was maybe driven by misguidance and my excitement to learning something new.
All thanks to my friend who hinted me a more effortless way to perceive this knowledge and concepts, and bring a common and practical understanding. If you are having a hard time understanding OOP, I hope this article helps you.