Abstraction in Object Oriented Programming (OOPS)

Abstraction in Real Life

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.

What is abstraction?

Abstraction is present in almost all real-life applications. For example:

  • When we log in to some online account, we enter the user id and password. After clicking the login button: how the input data is sent to the server and verified is all abstracted.
  • When we type on our computer, we press keys to get results on our screen. We know nothing about the mechanism that produces the output.
  • In Java, HashMap provides methods to store and retrieve key-value pairs from the map. So only information we need to use the hash map is to identify its use case of it in our application. How it is implemented inside, we are not required to know it.
  • Suppose we are designing a class to interact with the database for performing some database operations. Now a client of our class need not be aware of database programming. They need to only know some of the details of the class to perform the database operations without deep knowledge of database programming.

Abstraction in Object-Oriented Programming

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!

  • An object contains some properties and methods. Using access modifiers, we can hide essential details and provide access to required methods and properties to the outer world. This is one of the general procedures to achieve abstraction in OOPS.
  • Abstraction involves defining objects as abstract “actors” that can perform some set of functions, change their state, and communicate with other objects.
  • We can also understand abstraction using inheritance. In an inheritance hierarchy, the parent classes contain general implementation, but the child classes include the more detailed implementation of the parent classes.
  • Abstraction is a core idea behind many fundamental oops principles like the Open-Closed Principle and Dependency Inversion Principle.

How do we implement Abstraction?

  • In Java, we implement abstraction using an abstract class and interface.
  • In C++, we implement abstraction using header files, access specifiers, and abstract classes. We can create abstract classes in C++ by using virtual functions and inheritance.

We will discuss abstraction in Java and C++ in separate blogs later.

Some critical concepts to explore!

  • How do we achieve abstraction in Java using abstract classes and abstract methods?
  • How do we achieve abstraction in Java using Interfaces?
  • When to use abstract class and interface in Java?
  • What is a C++ abstract class?
  • What is a pure virtual function in C++?

Types of Abstraction in OOPS

Object-oriented programming can be seen as an attempt to abstract both data and control. So there are two types of abstraction in OOPS:

Data Abstraction

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.

Control Abstraction

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.

  • Control abstraction follows the DRY principle, which means Don’t Repeat Yourself and using methods in a program.
  • Using control abstraction, we can build new functionalities and combine control statements into a single unit.
  • We emphasize more how a particular functionality can be achieved rather than describing each detail.

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.

Abstraction vs. Encapsulation Comparison

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!

The basic difference between Abstraction and Encapsulation

Abstraction vs. Encapsulation

Advantages of Abstraction

  • Abstraction is used to create a boundary between the application and the client code. This could help us to reduce the design and implementation complexity of software.
  • In complex software, abstraction helps us separate responsibilities into software entities (classes, method, etc.) that only know the required functionality of each other but not how that functionality is implemented.
  • Abstraction maximizes ease of use for the relevant information. In other words, the code with proper abstraction help programmers to quickly make sense of what that code does and what it is meant to be used for. It avoids code duplication and increases code reusability.
  • Abstraction allows the programmer to simplify programming and shift focus from implementation details of actions toward the classes, available methods, etc. It will enable the user to avoid writing low-level code.
  • It allows the programmer to change the internal implementation of methods or concrete classes without hampering the interface.
  • Using abstraction, we can increase the code security as only relevant details will be provided to users.

Conclusion

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!

More from EnjoyAlgorithms

Our weekly newsletter

Subscribe to get free weekly content on data structure and algorithms, machine learning, system design, oops design and mathematics.

Follow us on:

LinkedinMedium

© 2020 EnjoyAlgorithms Inc.

All rights reserved.