DEV Community

raquelhn
raquelhn

Posted on

Objected Oriented Programming - the 4 Pillars! A-PIE

The first thing to understand about OOP is that we are taking real objects and we are representing them in code. For a simple example let's imagine we are talking about a PC monitor, and changing the brightness, turning it on and off would be considered as methods. To understand this post is important that you are already familiar with a programming language that allows you to create classes.

The fist pillar we are going to talk about is ABSTRACTION, it means to only show the necessary details to the user of the object. In the case of the monitor, if the user wants to turn on the monitor, he/she does not care about how it works they just want to push the button and turn on the monitor. In a coding context....this would mean we only care about calling the method and not about the method implementation.

The next pillar is INHERITANCE, this basically allow us to have code reusability. For this case let's remenber there are super classes and the classes the derived from them are called subclass/extended class/child class. These classes can inherite the methods from the parent, but they can also modify them to their specific needs in their respective child classes.

And the next pillar, which is probably the most challenging to understand is...POLYMORPHISM, it comes from POLY (means many) and MORPH (means more)...it allows us to decide what function to run once the program is running, this means once a method is called it follows a hierachy, first it would check with the parent class and then it would follow into the child classes (that are being called), and run the method and its respective modifications in the child class/classes.

The final pillar we are going to talk about is ENCAPSULATION, it involves to restric access of certain properties of our object, for instance is bad practice to allow the main that is calling your object to directly change/assign variables values, to avoid this we can modify the variable declaration as private in its child class. If we want to modify or get the variable the correct way would be to use a getter and setter functions to give the user the control to this (a future post would look more into this). In this way we are encapsulating our properties within the object.

I hope this helps clarify things!

Top comments (0)