Saturday, April 27, 2024 20:00

Table of contents >> Object Oriented Programming > Abstraction

Abstraction

Another fundamental principle of Object Oriented Programming is abstraction. Abstraction mainly refers to the ability of using something without knowing or being interested about how that something does what we request of it. We do this every day: we do not care how a computer does all it does (well, actually, some of us do), we only care that it is able to do what we need from it. We do not care how electricity works, is generated, or is implemented in the walls, we only care that we can turn on the light when we flip a switch.

One benefit of abstraction is the modularity of code, the so called “black box”. When we finish a module for logging in, we can use abstraction to make it self-sufficient. We don’t need to look inside it all the time, to see how it works, we know it works and we only need from it the action of logging in, and we can perform that action regardless of the way used to log in. When we finish a module for reading or writing files, we can use abstraction in the form of “storage device”, which allows us to perform the action of reading and writing files, regardless if we do it on a USB stick, a hard drive, etc.

Abstraction may seem very much similar to encapsulation, but it is not. Encapsulation is data hiding (information hiding, “you don’t need to know about this at all”), while abstraction is detail hiding (implementation hiding, “you don’t need to know how this works”).

There are three main advantages of abstraction: first and foremost, legibility of code comprehension. We are not troubled with details that do not concern us, we know that anything that reaches us was intended to be used, and we don’t need to dig through endless code until we understand how an action should be performed. Second, it encourages code re-usability and avoidance of code duplicates. Third, it empowers security, since users are only allowed to modify things that they should.

Abstraction can be achieved in two ways: either by using interfaces, of which we will learn in a future lesson, or through abstract classes and methods. I will discuss them in the next lesson.

Tags: , , ,

Leave a Reply



Follow the white rabbit