In philosophical terminology, Abstraction is a general thought process to understand the behavior or structure of an object. For example, our minds work based on abstractions. When thinking about a car, mostly, we remember an abstract concept of a car and its functionalities. This is why we can recognize an object like a car, even if it is different from any other car we have seen previously. In other words, we develop concepts of everyday objects by the process of Abstraction, where we remove unnecessary details and concentrate on the essential attributes and functionalities.
Abstraction is present in almost all real-life applications. For example:
In OOPS, Abstraction exposes necessary functionality to external objects and hides implementation details. This helps programmers use complex logic inside an object without understanding its implementation. In other words, it is the process of hiding the internal details of an application from the outer world so that we only need to know what the code does, but not how it does it!
Critical ideas to think about!
We will discuss abstraction in Java and C++ in separate blogs later.
Some critical concepts to explore!
Object-oriented programming can be seen as an attempt to abstract both data and control. So there are two types of abstraction in object oriented programming: Data abstraction and Control abstraction.
In OOPS, data abstraction is about the clear separation between the properties of a data type and the concrete details of its implementation. To achieve this, we can create data types in a class, design operations to work with the data type, and hide all the implementation details from the outside world. In other words, when an object's data is not available to the outer world, it creates data abstraction. If required, we can provide access to the objects’ data via some methods.
In other words, data abstraction provides an interface to the data type that can be visible to the client to use it. At the same time, inside the class, we can keep the operations working on that data entirely private, which we can change later for further updates or efficiency improvements. The idea is that any such changes in the concrete implementation are not supposed to impact client code.
For example, one can define an abstract data type called Dictionary, where each key is associated with a unique value, and we can access values based on their keys. Such data structure may be implemented using a hash table, a binary search tree, or even a simple array. For the client code, the type of abstract properties is the same in each case.
In OOPS, control abstraction is the abstraction of actions. Usually, we don’t need to provide details about all the methods of an object. In other words, when we hide the internal implementation of the different methods related to the client operation, it creates control abstraction.
We can also understand it differently at the low level. A software code is a collection of methods written in a programming language. Most of the time, some methods are similar and repeated multiple times. So the idea of control abstraction is to identify all such methods and bundle them as a single unit.
Note: Control abstraction is one of the primary purposes of using programming languages. Computer machines understand operations at a very low level, such as moving some bits from one memory location to another location and producing the sum of two sequences of bits. Programming languages allow this to be done at a higher level.
A well-designed code with proper use of abstraction follows the Principle of Least Astonishment: “A component of a system should behave in a way that most users will expect it to behave. The behavior should not surprise users”.
But the critical question is: how to identify and expose that expected behavior to the users? How to handle their implementation details? At this stage, the next pillar of object-oriented programming comes into the picture: encapsulation!
Difference between Abstraction and Encapsulation
So Abstraction exposes only the details concerned with the client or user. If the client is using some class, they need not worry about the inner details related to the operations of the class. In other words, it helps us to decouple the user of the object with its implementation. So in this way, the behavior of an object is easy to understand and maintain. As if there is any change in some operation, we only need to change the inner details of the related method.
Enjoy learning, Enjoy OOPS!
Subscribe to get weekly content on data structure and algorithms, machine learning, system design and oops.