Introduction
One important concept that should be available in a software developer's repertoire is a working understanding of object-oriented programming(OOP).
OOP (Object-Oriented Programming) is a way of writing code that organizes it into reusable "objects" that contain both data and the functions that can act upon that data.
Before the advent of OOP, procedural programming was the more heavily used programming paradigm.
Procedural programming is a style of writing programs where the programs were divided into smaller functions with variables that store associated data. Although procedural programming worked well in smaller programs it had fundamental limitations, one of the main limitations was that as programs grew bigger, the number of functions and variables grew bigger which made the programs unwieldy and prone to breaking at the slightest changes, in other words, spaghetti code. This is where Object-Oriented Programming comes in.
In this article, we are going to explore the four pillars of OOP:
Encapsulation
Abstraction
Inheritance
Polymorphism
Prerequisites
To understand this article the reader has to have an understanding of Javascript concepts, such as:
Functions
Variables
Arrays
Objects
Data Types
What is Encapsulation?
Encapsulation is a process where a function and its associated data are fused into one object allowing for better organization. Encapsulation is beneficial in writing programs because it enables the developer to protect the data of the object and prevents it from being accessed or changed by external code, this helps prevent unintended changes that might compromise the integrity of a program. Another benefit of encapsulation is that it prevents the spaghetti code phenomenon. It makes our code a lot more organized and easy to read making debugging a lot easier.
An example can be seen in the code block below:
The example above shows the clear difference between functional programming and OOP. The getWage function uses the variables above it as its parameters and thus is subject to external change whereas the getWage method uses the properties of the employee object as its parameters which makes the code more readable, organized, and less prone to external change.
What is Abstraction?
Abstraction is the process of hiding unnecessary details and focusing on the essential features of an object.
For example, pictured above is a DVD player. The DVD player has an interface with buttons for play, pause, stop, etc. Inside the DVD player is a complex logic board that controls the operations of the DVD. When the DVD is in use, the user only interacts with the available interface and has no interaction with the logic board. This is abstraction in practice. This same principle can be applied to objects where we hide specific methods and properties from the outside. This enables us to have simpler interfaces and reduces the impact of change because when an object is abstracted, we deal with just the interface which reduces the possibility that the changes we make would have an impact on the object.
What is Inheritance?
Inheritance is the process of creating a class from another class to create a hierarchy of classes that share the same properties and methods with the possibility of the child class having custom properties and methods. This process is carried out using the extends keyword as shown in the example below.
The example above shows the Cat class being extended from the Animal class and inheriting its properties while having a slightly different sayHello method.
Inheritance is remarkable because it enables us to keep our code efficient and reusable. This is because instead of creating multiple classes with the same properties and methods over and over again which would be tedious and time-consuming, inheritance allows us to create one parent class and extend multiple child classes from that parent class. Inheritance is also the reason why polymorphism works which we will see below.
What is Polymorphism?
Polymorphism is the concept of having an interface that can be used by multiple objects that have that same interface. Polymorphism provides us with the ability to call the same method on varying objects as shown in the example below.
The example above shows the Dog and Cat classes extended from the Animal class, with both child classes having similar methods and properties to the parent class with a few custom tweaks.
Let's see what happens when we create new objects from these classes, and call the sayHello method on each object with a loop.
Even though the Cat and Dog classes had the same methods the result of calling the method is different. This is because of polymorphism. Polymorphism allows our objects to have varying forms.
This allows our code to be reusable, flexible, and efficient. Polymorphism allows our code to be reused across multiple classes, making it easier to maintain and update our codebase. It provides flexibility in our codebase by allowing us to create properties and methods specific to various classes all while ensuring that we do not have to duplicate our code.
Conclusion
To recap, Object Oriented Programming is a method of writing code with the use of objects that makes our code flexible and reusable. As a software developer, one of your overarching tasks would be to find ways to make processes more flexible, and more streamlined and OOP is one way to achieve that. The best way to truly understand OOP is to practice as much as possible with it. Build projects with it, Solve coding to challenges with it and you will attain mastery of Object Oriented Programming.