OOP Concepts — Abstraction

In this article we will discuss one of the 4 pillars of Object Oriented Programming: Abstraction! We will talks about what it is, different types of abstraction, and an example using JavaScript. Before we dive into it, let’s take a look at what Object Oriented Programming is and why it’s important.
Object Oriented Programming
Object oriented programming is a model that organizes software design around objects. It allows us to focus on the objects that we want to manipulate, rather than the functions we use to manipulate them. This is very useful when creating large applications that are complex and actively maintained by developers. OOP is broken down into 4 key components: Classes, Objects, Methods, and Attributes. Together, these components can successfully work together to create code that is well-structured, easy to read, and reusable.
There are 4 pillars to Object Oriented Programming that you should always keep in mind when following the OOP model. Today, we will be discussing Abstraction, which is one of those 4 pillars.
Abstraction
What is Abstraction?
Abstraction is a way to organize our code to only show the user the necessary details of our object, or really, what’s important to them. This process hides internal data and implementation. Often times in programming, there is a lot of background functionality that either the user doesn’t want to see, or we don’t want them to see it. We only show the important stuff to create a boundary between the application and our client programs. We provide access only for required properties and functions to other pieces of the application.
Types of Abstraction
There are two types of Abstraction:
- Data Abstraction
- Process Abstraction
Data Abstraction
Data Abstraction is when the data of an object is not visible to the users. Access to the objects data can be given to the user through the use of methods within our object.
Let’s take a look at an example of Data Abstraction using JavaScript:
var Book = function(id, title) {
this.id = id;
this.title = title;
this.getTitle = function() {
return this.title;
};
this.getId = function() {
return this.author;
};
};
This is an example of Data Abstraction because if we made the id and title of the Book object private, the only way to access them would be through the use of our getTitle or getId method.
Process Abstraction
Process Abstraction is when we hide the internal implementation of the different functions involved in an operation. We do not need to show the user how we got the answer, we just need to show them the answer.
Let’s take a look at an example of Process Abstraction in JavaScript:
const Square = function(sideLengths) {
this.sideLengths = sideLengths;
this.area = Math.pow(this.sideLengths, 2)
}
var mySquare = new Square(4);
console.log(mySquare.area)
// returns 16
In this example, we are hiding the process for which we calculate the area of the square. We are given the side lengths of the square when the object is instantiated, which we then use to calculate the area of the triangle. The user can only see the area, not the formula we used to calculate the area.
Conclusion
In this article we discussed Abstraction, which is one of the 4 pillars of Object Oriented Programming. We discussed what abstraction is, the two types of abstraction, and how we can utilize this design using JavaScript. OOP design is used everywhere, and is a key component to learning clean software design. Understanding these common concepts have helped me dive deeper into what software engineering is, and how to maximize my coding potential. I hope you enjoyed reading, and keep an eye out for my future articles. Keep learning, and happy coding!