4 Pillars of Object Oriented Programming?

Gerald Tee
4 min readOct 14, 2020

Going through Flatiron School, one of the first main topics they taught us was Object Orientation in Ruby. Object Orientation is a way to bring metaphors of real life concepts into our code called “objects”, and these objects would represent that real life “thing” and have its own properties and methods. For example, a person is born with properties such as a name, age, ethnicity, etc., and can perform actions such as walk, talk, wave, etc. In our code, we can create an object that we name Person, and create instances of the Person object to represent people with those initial attributes, and build methods on the object which defines the actions a person can do.

All projects I created in the bootcamp used Object Orientation, and although I learned what it was in a nutshell, there was more to it than that. So on further research I came across the 4 Pillars of OOP (or APIE), which are explained at a high level for my understanding.

  1. Abstraction

Abstraction is only showing the necessary details, properties or methods of an object, and hiding the complexity behind any underlying implementation or whatever would be unimportant. We only want the necessary portions of a class to be visible on the user end, or even certain parts of the application.

The idea of it is analogous to movement in a video game, where a player doesn’t need to know the implementation behind how their main character moves, how they were built or what code was behind it. That’s abstracted away from the user, and they would only need to know what keys to press in order to move their character.

2. Encapsulation

In the name, I really think of encapsulating as a pod or sort of putting food into a container, or just “encapsulating” something. This is when we group properties, variables and the functions into an object, which keeps them in one place to reduce complexity and increase reusability. This also restricts access to the class and properties and methods of our objects, which is called data or information hiding.

The object should not have its properties changed by anything outside of that object or calling on that object, for many reasons including safety and other parts of the application to work properly. This is where getters and setters come in, as well as private methods.

3. Inheritance

Inheritance is when one child object or class can inherit properties and methods of its parent object or class. This eliminates redundancies in code by making code reusable. For example, instead of having several objects or classes that contain the same functions, we can have a general object with one function that other objects can inherit and use, to reduce repeated code. The classes that contain these reusable functions are the super class, parent class or base class, while the classes that inherit those functions are the subclass, extended class or child class.

4. Polymorphism

This one seemed the most advanced topic. Polymorphism means “many forms”, and one simpler explanation is that while a program is running, we can determine what functions to run. I think the best example for me was using the + sign, which can add integers, but also concatenate two strings together. Based on what data types is provided, the + sign will determine what to do during runtime and return the result without us specifying.

For different objects, they can have many forms. Say that we have two subclasses that extends a parent class. In all three classes, we have a yell() method. The parent class returns “Hey!”, while the subclasses’ yell() method returns something different. If we invoke the subclasses’ yell() method, then in runtime, the program will also look at its parent class’ yell() method, compare the difference, and override the parent if it’s different. This shows how some objects can have the same methods, but return something different.

Another benefit of polymorphism is avoiding or refactoring long blocks of code like long if/else or switch/case statements so that code is more concise and readable, by using the render() method.

This is just an overview of what the 4 pillars of OOP meant in my quick research, and I can now see how it’s important for better code and why my bootcamp stressed this topic so much.

Sources:

The Four Pillars of Object Oriented Design — Code Project

Object-oriented Programming in 7 minutes | Programming With Mosh

Object Oriented Programming — The Four Pillars of OO — Keep On Coding

Object Orientation — LV (2nd Recording)Link — YouTube

--

--