Table of contents
1.
Introduction
2.
What is Abstraction in OOPS?
3.
Example of Abstraction in OOPS
3.1.
C++
3.2.
Output
4.
Types of Abstraction in OOPS
4.1.
Data Abstraction in OOPS
4.2.
Process Abstraction in OOPS
5.
Abstraction using Classes
6.
Abstraction using Header files
7.
Abstraction using Access Specifiers
8.
Why Do We Need Abstraction in OOPs?
9.
Advantages of Abstraction in OOPS
10.
Disadvantages of Abstraction in OOPS
11.
Frequently Asked Questions
11.1.
What is Object Oriented Programming?
11.2.
What are the 4 basics of OOP abstraction?
11.3.
When to use abstraction?
11.4.
Why do we need OOPs?
12.
Conclusion
Last Updated: Jan 2, 2025
Easy

Abstraction in OOPS

Author Anant Dhakad
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In programming, object-oriented programming seeks to implement real-world concepts such as inheritance, hiding, polymorphism, etc. The main goal of OOP is to connect data and the functions that operate on it so that no other part of the code may access the data except that function. In this blog, we will learn about Abstraction, one of the fours pillar of OOPs.

Abstraction in OOPS

What is Abstraction in OOPS?

In C++, data abstraction is one of the most significant and crucial features of object-oriented programming. Abstraction refers to revealing only the most essential information while hiding the details. Data abstraction refers to exposing only the most essential aspects of the data to the outside world while hiding the implementation details.
Consider an example of a man driving a car. The man only knows that pressing the accelerators will increase the car's speed and that applying the brakes will stop it. Still, he has no idea how the speed is increased by pressing the accelerators, nor does he understand the car's inner mechanism or how the accelerator, brakes, and other controls are implemented. This is precisely what Abstraction is.

Example of Abstraction in OOPS

  • C++

C++

#include <bits/stdc++.h>
using namespace std;

class ExampleAbstraction{
   private:
       // Here we have hidden (made 'a' & 'b' unaccessible) from
       // the outside world.
       int a, b;

   public:
   
       // method to set values of
       // private members
       void set(int x, int y){
           a = x;
           b = y;
       }
       
       void display(){
           cout<<"a = " <<a << endl;
           cout<<"b = " << b << endl;
       }
};

int main(){
   ExampleAbstraction obj;
   obj.set(30, 40);
   obj.display();
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

a = 30
b = 40

Types of Abstraction in OOPS

In object-oriented programming, two major types of abstractions exist.

  1. Data Abstraction in OOPS
  2. Process Abstraction in OOPS

Data Abstraction in OOPS

Data abstraction in object-oriented programming (OOPS) focuses on hiding the implementation details of data objects and only revealing the essential features or functionalities. It allows developers to define a complex data type by providing a simplified interface for interacting with objects. Through encapsulation, classes can conceal the internal state and expose only necessary operations, promoting modularity and reducing complexity in software systems.

Process Abstraction in OOPS

Process abstraction in OOPS involves hiding the intricate details of how a particular task or operation is performed within a system. It emphasizes defining methods or functions that encapsulate a series of steps or algorithms, without revealing the underlying complexities to the user. This abstraction level enhances code reusability, promotes separation of concerns, and facilitates easier maintenance and modification of software components. By abstracting processes, developers can achieve clearer, more efficient designs that enhance the overall functionality and scalability of applications.

We can implement abstraction in several ways. Following are a few methods to implement abstraction in C++.

Abstraction using Classes

Abstraction can be implemented using classes in C++. We can group data members and member functions in a class using the available access specifiers. A Class can choose which data members are exposed to the outside world and which are hidden.

Abstraction using Header files

Abstraction is also implemented using Header files. For example, we use many mathematical functions present in math.h header file. We use the function pow() in the math.h header file whenever we need to determine the power of an integer and supply the numbers as arguments without knowing the underlying algorithm.

Abstraction using Access Specifiers

In C++, access specifiers provide the foundation for implementing Abstraction. We can use access specifiers to impose restrictions on class members. Access specifiers in C++:

  • Public: members and methods declared as public can be accessed from anywhere in the program.
  • Private: members and methods declared as private can only be accessed within the class.

Using the two properties provided by access specifiers, we can easily implement Abstraction. For example, in a class, the members that specify the internal implementation can be defined as private. Important information that needs to be shared with the outer world can be public. And because they are inside the class, these public members have access to the private members.

Why Do We Need Abstraction in OOPs?

Abstraction is a fundamental concept in Object-Oriented Programming (OOP) that focuses on hiding the implementation details and exposing only the essential features of an object. It allows developers to work at a higher level of complexity while simplifying interactions with underlying systems.

  1. Simplifies Complexity: By hiding unnecessary details, abstraction makes it easier to understand and manage complex systems.
  2. Enhances Code Reusability: Abstract classes and interfaces promote reusable code by providing a blueprint for implementing derived classes.
  3. Encourages Modularity: Abstraction divides a program into independent modules, enabling better maintenance and scalability.
  4. Improves Security: By exposing only the required functionality, abstraction helps protect sensitive information and implementation details.
  5. Facilitates Focused Development: Developers can concentrate on high-level design and essential features without being bogged down by intricate details.

Advantages of Abstraction in OOPS

Following are the main advantages of Abstraction:

  • Increase reusability of code and avoids its duplication.
  • It helps in making the application secure as only essential details are provided to the user.
  • We can change the internal implementation of a class without affecting the low-level code.

Disadvantages of Abstraction in OOPS

While abstraction offers numerous benefits, it also has certain drawbacks that developers should consider:

  • Increased Complexity in Design: Implementing abstraction often requires designing abstract classes and interfaces, which can be time-consuming and complex.
  • Performance Overhead: The abstraction layer introduces additional processing, which may lead to a slight decrease in performance, especially in resource-intensive applications.
  • Learning Curve for Beginners: Understanding and implementing abstraction can be challenging for novice programmers, particularly when dealing with advanced OOP concepts.
  • Potential Over-Abstraction: Excessive use of abstraction can make the code overly modular and harder to follow, resulting in reduced readability and maintainability.
  • Debugging Difficulties: Since abstraction hides implementation details, debugging and tracing errors can become more challenging.
  • Dependency on Implementation: Changes in the underlying implementation can still impact the abstract layer, necessitating updates to maintain compatibility.

Frequently Asked Questions

What is Object Oriented Programming?

Object Oriented Programming (OOP) is a programming paradigm in which the entire software is composed of a collection of objects that communicate with one another. A collection of data and methods that operate on that data is referred to as an object.

What are the 4 basics of OOP abstraction?

  1. Encapsulation: Bundling data and methods together to restrict direct access.
  2. Data Hiding: Concealing implementation details of data objects.
  3. Modularity: Designing software components that can be independently developed and maintained.
  4. Polymorphism: Providing a single interface to entities of different types or behaviors.

When to use abstraction?

Abstraction is used to simplify complex systems, promote reusability, enhance maintainability, and facilitate efficient software development.

Why do we need OOPs?

OOPs enables modular, reusable, and maintainable code through concepts like encapsulation, inheritance, and polymorphism, leading to efficient software development and easier maintenance.

Conclusion

In this article, we learned about Abstraction in OOPS. It serves as a cornerstone for creating efficient, modular, and maintainable software. By hiding complexity and emphasizing essential details, it enhances code reusability, promotes clear design patterns, and facilitates the management of large-scale applications. 

Recommended Readings:

Live masterclass