Object-oriented programming sounds like a big deal. But what's the alternative? What have we been implementing so far?
Principle programming approaches
There are two general approaches:
Procedural programming is what you likely already know - that is implementing logic as a sequence of instructions using variables, control flows, and functions.
The best way to organize the code using procedural approach is using functions and that’s the limit!
For example, if we want to create a program to count steps for people, we'd need to store some data for each person and count their steps. We could use individual variables to store different kinds of data, such as name, number of steps, distance or improve it a bit by grouping those variables in arrays and dictionaries and their compositions. Then we could write abstract functions to perform various manipulations as needed. As we know we could make them reasonably general by using parameters.
In some cases that would be sufficient, however, in most cases it's very inefficient and a sure way to make mistakes.
What can make it more efficient?
Utilizing object-oriented approach can take us much further than just a code optimization by using variables and functions. It enables us to take advantage of another level of optimization - which is contextualizing those variables and functions.
As the name suggests, it has something to do with objects.
What is an object?
We can define an object as a set of variables and functions grouped together by some context. An example of an object from the real world is a label or a button. A more abstract example would be a process, such as a gesture or an animation.
An object's variables are called properties, and they describe that object. An object's functions are called methods, and they manipulate that object.
Let's look at a couple of examples:
1. Something concrete, like a smart phone:
it has a number of visible characteristics, or properties:
... and hopefully some more ;)
2. Something more abstract, like a touch gesture:
it has a number of defining characteristics, or properties, which are intangible this time:
the duration of a touch
number of fingers
number of touches
do something when gesture detected
3. And ultimately, endless opportunities for object compositions:
a smartphone can support gestures;
a gesture can be described with a touch (or multiple touches). And, a touch, in turn, can be defined as an object.
4. Referring back to our personal step counter example, we can describe it with properties:
Grouping characteristics and functionality that surround an object allows us to refer to attributes and execute functions in the context of that specific object!
Let's review it on a comparison diagram:
What else are objects good for?
What's that supposed to mean :o?
Let’s look at a reasonably complex physical object: a car. Perhaps you drive one every day (or occasionally). When you want to turn left or right, you simply turn the steering wheel in the desired direction. You don’t have to move all the individual parts of the turning mechanism to turn the wheels to make the car turn as a result. And, ANYONE (with a driver’s license :ange:) can drive that car - utilize the car object, not just the original manufactures, or the developers of that object!
When something needs a repair you are likely going to use professional services - the “developers” who specialize in car mechanics.
If you want to install a fancy audio system, you’re also likely to purchase one available on the market (another object), maybe customizing it to your liking, but again, made by audio systems “developers." Once the wires are connected - integrated to your car object - you are left to safely press the buttons without concerning yourself with the particular functionality of the electronics inside.
Not only is it simpler for the developer of an object to navigate and manipulate the code, but it also allows other developers to utilize the exposed functionality of an object (like a steering wheel in a car or buttons on an audio player) without the risk of damaging it accidentally or even having to know what’s going on behind the scenes.
The concept of “hiding” the actual implementation from a regular viewer (like car mechanics from a driver) in OOP is called Encapsulation.
The next principle is called Abstraction - which is making something independent (or, less dependent) on the particulars of a certain context.
For example, if we were to create a function that would calculate a sum of 2 integers, It would be independent of particular numbers, whether it’s
2 + 2 or
10 + 1 . Our function wouldn’t care what those numbers mean or where they come from. All we need to do is to implement a function with 2 parameters and return the result of their sum.
We just took advantage of abstraction! We created a function that is independent from the numbers we need to add.
In the context of objects, abstraction and encapsulation go hand in hand together. In our car example, we are hiding the implementation of many aspects, like the turning mechanism. At the same time abstracting it, as we can turn the same way many types of cars and don’t have to implement the turning mechanics individually for each car.
Procedural programming is using variables and functions.
Object-Oriented programming is using Objects to contextually group related variables and functions.
Object-Oriented approach provides for flexible usability, security, stability and much easier maintenance of the code. This concept is defined by two principles of OOP - Encapsulation & Abstraction.
Variables that describe objects are called properties.
Functions that apply to objects are called methods.