OOP and its four pillars(a pharmacological approach to understanding it).

OOP and its four pillars(a pharmacological approach to understanding it).

·

7 min read

Object-oriented programming can be a hard pill to swallow and when it comes to understanding it at its core, its 4 pillars must be well understood. Just like you, I had a tough time understanding this concept. So to make your head hurt less than mine, I have decided to put this piece together.

There are four pillars of object-oriented programming. Abstraction, encapsulation, inheritance, and polymorphism. To explain them, I am going to use drug pills as an analogy.

So, clears throat when you are down with a headache most likely you will find yourself popping a pill to numb that headache.

1_8pJFNKSkdUjbHWh62vkcgQ.jpeg

We start with the first pillar.

Encapsulation:

So, you are down with a headache, you go to your favorite pharmacy, you pay for your drug of choice. At home, you pop that pill and swallow it. Did you realize that capsule could have been several tiny pieces of pills each holding their respective chemical compounds and all? rather the pharmaceutical company decided to make them all into a “capsule”.

Safe to say the tiny pills were "encapsulated" into a capsule. Sort of a one-stop approach to treating the ailment. Swallow one pill and all is well. You see, that capsule also hides from you the number of compounds that may have been mixed to get you the relief you seek. Frankly, you don't even need to know how the contents in the pill were ratioed to give you the desired relief.

In programming, encapsulation is simply a way of organizing a class's methods and attributes so we have all our code in one structured blueprint as well as keeping away the unnecessary burden of knowing how the implementation of the code works from the user. When we use “.split()” for instance on a string, we really do not need to know how the splitting is done. we just know when we call it, we split a string into an array. With encapsulation, we are not bothered by how the internal working of the drug takes effect.

Abstraction:

When we go to buy our pill, we simply know that for it to work, we swallow the pill and take it probably 3 times a day for it to relieve us of the ailment. Here you see we have 2 “methods” to this drug at our disposal: the “swallow()” method and the “dosage()” method which is 3 times a day. This is the level of abstraction the company creates for the drug. We do not need to know why we have to swallow and not chew or why the dosage is 3 times. We just know we swallow 3 times a day.

With abstraction, we are simply exposed to the general workings of the drug just as we would with code. We know that for example if we want to get the length of an array we use “.length” for example or “len()” if we are using python. Abstraction simply put gives a more generic perspective to the class without more.

Inheritance:

At some point in our lives, we may have used oxycodone. This is a very powerful pain relief medication that is a derivative of codeine. Codeine is primarily used as a cough suppressant. I must apologize for this brief detour to pharmacology but we will get the point in the end..lol.

So now oxycodone is a more powerful pain relief medication but it also “inherits” codeine's cough suppressant effect. This is an example of inheritance. The cough suppression occurs in the brain stem while the pain relief occurs in the higher centers of the brain.

So if for example you had the flu and while going down the stairs you fell and seriously sprained your ankle, what oxycodone would do for you is rather than buy two pills one for the flu and one for the pain, oxycodone comes with the payload of both pain relief and cough suppressant. In this case, the pharmaceutical company with one pill has solved 2 issues for you. What is at the base of oxycodone? codeine. Oxycodone as a pill simply inherits the benefits of codeine! This way, we can now say that codeine is the "base class" from which oxycodone derives from.

Polymorphism:

Polymorphism simply means taking different forms. This is another beauty of OOP. Objects are instances of a class having distinct forms. For this, I am going to use 2 drugs, levofloxacin, and ofloxacin, which are both antibiotics and derive from a base compound called fluoroquinolones (I am sure you are wondering if this is still a coding piece? Hang on!)

Levofloxacin and ofloxacin are polymorphic and are mirror images of each other(enantiomers). Whereas ofloxacin is a narrow-spectrum antibiotic, levofloxacin is a broad-spectrum antibiotic. They are both fluoroquinolones but have different uses. This is an example of polymorphism, where a single compound has different attributes based on its chiral arrangement. So even though both drugs are of the same base, their “attributes and methods” differ based on their chemically configured chiral arrangements.

If your hard hurts look on the bright side, we just learned a little chemistry.

Conclusion:

Hopefully, this analogy helped you. OOP is a shift from procedural programming where things flow in a linear form. In OOP we are able to model our problems using various blueprints called classes the sole purpose of which is objects which are distinct in features but common in origin.