4 Pillars in Object-Oriented Programming

Toni T Diep
3 min readOct 15, 2021

Welcome to the Baby Steps Series on Computer Science Concepts: 4 Pillars from Object-Oriented Programming (OOP), with Toni! I blanked — could not answer this during a technical interview (my first) live coding over Skype, post bootcamp. Utilizing a tutorial video by Programming with Mosh, here, as a refresher to get started.

The 4 Pillars are Encapsulation, Abstraction, Inheritance, and Polymorphism

Prior to OOP, writing code can become messy. For example, when written functions are everywhere, resulting in spaghetti code. So OOP is the solution to this. In OOP, we will group related variables and functions that operate on them into objects, which is also known as Encapsulation.

Browser’s localStorage object has a property such as ‘length’ returning the number of objects in the ‘Storage’ with other methods such as ‘removeItem’ and ‘setItem’.

Where we stored data locally. In our console from the browser — From Mosh’s tutorial

Let’s take the OOP approach in Encapsulation with this info! In the code snippet below, this is the Procedural Example (variables on one side and function on the other side to calculate one’s wage).

“The best functions are those with no parameters!” — Uncle Bob — Robert C. Martin

//Procedural Programming: In the code snippet below, this is the Procedural Example (variables on one side and function on the other side to calculate one’s wage).//variables
let baseSalary = 30_000;
let overtime = 10;
let rate = 20;
//line below holds parameters
function getWage(baseSalary, overtime, rate) {
return baseSalary + (overtime * rate);
}
////////////////////////////////////////
//Better option in OOP (below) making maintainance of these functions, easier!
//No parameters here and a single unit!
let employee = {
baseSalary: 30_000,
overtime: 10,
rate: 20,
getWage: function() {
return this.baseSalary + (this.overtime * this.rate);
}
};
//a in single unit with fewer and fewer parametersemployee.getWage(); //30200
Property f(). Method x from Mosh’s tutorial

Abstraction — we do not care what is on the inside. Analogy think of a DVD player with a few buttons on the outside versus the complexities inside with the mother board! Hidden properties and methods, giving us some benefits.

Benefits:

  1. with a simpler interface object with a few properties and methods versus an object with more than a few properties and methods.
  2. Reducing the Impact of Change — which will not be leaked outside. The rest of the application’s code will not be negatively impacted.

Inheritance!! — a mechanism that eliminates redundant code.

Polymorphism — Poly meaning many and morphism meaning form. This technique helps us remove long if/else statements, and also switch/case statements.

In summary, the benefits of OOP are listed:

  1. Encapsulation: Reduce complexity. Increase reusability. Group related variables and functions together.
  2. Abstraction: Reduce complexity. Hide details. Only show what is important, while isolating the impact of changes.
  3. Inheritance: Eliminate redundant code.
  4. Polymorphism: refactor lengthy if & else statements, as well as switch/case statements.

Thanks for reading!

--

--

Toni T Diep

multilingual Software Engineer, always learning and growing.