Learn the four pillars of Object oriented programming, with human nature

Learn the four pillars of Object oriented programming, with human nature

ยท

4 min read

Hello dev ๐Ÿ‘‹, in this post i will explain the four pillars of OOP with you (human nature) as an example.

In other words, we will talk about the four pillars of OOP by actually looking at where they are implemented in our own body system. So let's dive in.

But What is OOP:

OOP is the short form of Object oriented programming.

It is a programming model based on the concept of objects.

In functional or procedural programming, you write functions that performs operations on data that is often globally accessible, but in Object oriented programming, you group or bind together those data and the functions that operate on them into objects so that (if you want) no other part of the code can access the data and functions of that object.

In OOP, those data can be called properties and the functions called methods.

"Object" in OOP can represents any real life object, so just know you can take it to be any real object of your choice (cars, TV, etc).

Four Pillars of OOP:

There are four main pillars which OOP is built on, they are like features that express what you can achieve through OOP. They are:

1. Inheritance:

Inheritance lets one object acquire the properties and methods or data and functions of another object.

2. Encapsulation or data-hiding:

Let's you remove access to some data or functions of an object. Making things private, that's just it.

3. Abstraction:

Let's you obscure or hide away the implementation details of an object's method (function) in order to simplify things conceptually.

4. Polymorphism:

This is the ability of a variable, function, or object to take on multiple forms. This means different objects may have functions with the same name, but with different behaviors.

Four pillars of OOP with human nature:

First we'll assume you're an object for the rest of this post (sorry no offense ๐Ÿ˜‰) but think about it; you have properties and methods too. ๐Ÿค”

Okay hopefully you're a human being, and you have some properties like eyes, mouth, teeth, hands, legs, hairs and you also have some methods or actions you perform like talk, walk, run and more especially eat, sleep, code ๐Ÿ˜„. So let's now see how these pillars of OOP applies to you.

1. Inheritance:

Let's say there's a Generic Human Object that houses common properties like head, eyes, mouth, tongue and common functions like eat, walk, talk, sleep, which normally every human being male or female should have.

Now you as a male or female (which is an object of another type, and a child of Human object), inherits these properties and methods from the Human object and can then add some other properties or methods which pertains to only male or female.

2. Encapsulation or data-hiding:

So you as a human child, there are some properties you have that you can't directly modify, these properties are only modified when you take some actions (methods).

An example of this is your energy-level property, you can't directly modify your energy level, unless you perform some action like eating. You get the point now?, Your energy-level is encapsulated/hidden away from you, so you can't modify it directly.

3. Abstraction:

Imagine if you took care of all that happens when you eat, you'd take care of chewing, swallowing, digesting, and performing other actions that eating food does to your body like incrementing your energy-level.

Now that'll be a very hard process, that's even if you can do it. and imagine you make a mistake in digesting the food, then you may end up killing yourself ๐Ÿ˜….

What the creator did was he made just a single function eat, that abstracts away the implementation details of those other methods (digest, etc), so when you eat (call the eat method), the eat method in turn automatically calls the other methods (digest, increase energy-level, etc) without you knowing. So you just have to eat, and every other process like digestion happens automatically.

4. Polymorphism:

Recall that male and female both inherited some properties and methods from the Human parent object.

Some of these functions have the same name in male and female objects, eg eat (you eat whether you're male or female.), but differs in how they work or are implemented.

An example of this is the walk method. Male and Female walks, but the way a male walks is/might be different from the way a female walks. Even tho they have the same function name "walk", their implementation differs. You can say that the walk method is polymorphic because it can take different forms. Other example of polymorphic methods are dance, speak, etc.

I will soon write another article on this exact topic, but with code examples. But hopefully you understood what OOP is all about and how it's four pillars are implemented in your own body. Thanks for reading and remember to share this post with your friends, family, and colleagues.