Currently, there are many languages support for Object Oriented Programming and being one of the most used paradigms, it is guided by 4 pillars.
Let us know them and understand them with simple examples.
But first, what is a paradigm?
It is a way of solving common problems in programming. Imagine it as a recipe to be able to prepare your food (your code).
After it’s clear, the content in Object Oriented Programming (OOP) is the code you’re going to write and its tools, we’re going to identify these as classes, attributes, methods, and the main protagonist, objects.
So they are pillars? No, but to understand pillars we have to first understand the parts that make up OOP, so calm down and continue:
Classes are the template with which we create objects. This mold has both attributes and actions that the object will be able to perform as soon as it is present. Imagine the mold you can make cookies with.
Attributes are the attributes that a class or object possesses. The size, color, smell or taste would be following the example of cookies.
Methods are actions that objects can perform. How worrisome to think about cookies that move, better imagine a fish, its ways would be swimming or eating.
When you use a template (class) and go back to the fish analogy, you create one, for example a clown fish, then you are already looking at an object that has its own characteristics(s) already has a more distinct identity with. more ways) and which you can take advantage of in different ways.
Well, understanding the above we are ready for the 4 pillars of OOP. Which are:
What does all this mean and how can we sum it up? We’ll learn below.
column of encapsulation
Basically, it means that a class has attributes and methods that cannot be accessed or edited from outside the class.
Imagine a bank vault (class) where it is sought to protect funds (features), only authorized personnel can enter to review or withdraw specific amounts, but no one is allowed to operate the vault. Should not change.
pillar of abstraction
Here we try to hide the complexity that a class can have in order to focus on “easier to use” functionalities.
Think of a TV as a complex device, can you imagine that in order to pass a channel you would need to know how each circuit works? Well, we are given a remote control in the form of an interface that separates us from that complexity and allows us to control it with a few buttons.
This is a process that creates a child class that inherits from the parent class. It allows you to share your methods and attributes. You can also overwrite or define a new one.
It is easy to add, you have physical characteristics inherited from your parents like color of eyes, hair, skin etc. But depending on your personality you will have a different style or different thinking (attribute and method override).
Objects that are born from classes that have common inheritance have the ability to change their form or their type as needed.
Let’s go back to the food, an egg has a simple structure, but it can be prepared in a variety of ways, fried, hot, crashed, etc. All these dishes are different, but they have the same origin, the egg (shared heritage).
Now with this theoretical knowledge you are ready to test all this in real code, but we will do that in a future post. I