What is Abstraction in OOPS? Definition, Types, and Advantages

By | October 30, 2023

What is Abstraction in OOPS?

Abstraction is an essential feature of Object Oriented Programming, which implies only keeping the essentials and removing unnecessary information. Abstraction in OOPS is used to hide unnecessary information and display only necessary information to the users interacting. It is essential to represent real-world objects in a simplified manner for users to interact easily. 

Let us consider a real-life situation where a man needs to withdraw cash from an ATM. He will insert his credit or debit card and enter the PIN, and the ATM will deliver the cash if the information provided is correct.

abstraction in OOPS

At the backend, many processes run during the complete process. The Bank will receive the request and check the customer’s account balance. If the balance in the given bank is sufficient, the machine will verify the PIN. It will process the transaction if the information is correct.

This complete process happens at the backend, which the user does not have to worry about. He only enters the bank PIN and withdraws the required cash. This is where abstraction comes in OOPS. It only displays the information that is necessary for the user, who will interact with the application or system. 

Types of Abstraction in OOPS

There are two major types of abstractions in object-oriented programming. 

1. Data Abstraction in OOPS

Data Abstraction is the most straightforward implementation of abstraction. When we are dealing with a lot of complex processes, we hide unnecessary data and display only the information necessary for the users. Classes define data members as attributes and functions as the method that is used to perform the required operation on data. 

abstraction in oops

Users interact with the system through method calls present in the classes. It keeps the complex operations hidden from the users during implementation. 

2. Process Abstraction in OOPS

When we hide the internal implementation and do not disclose all the details about a method or function to the users, this is known as Process abstraction. In this type of abstraction, instead of dealing with data, we deal with their process. 

Let us go back to our previous example, where a man has to withdraw some amount from an ATM. The man only needs to enter the correct PIN, while there is a whole lot of processing which goes on inside. This is process abstraction, where we keep the details of the implementation methods hidden. 

Also check: Features of C++ programming language

What is Abstract Class in OOPS?

Abstract classes are used to implement one or more abstract methods in OOPs. The abstract class is a blueprint for other classes that can be used to abstract virtual as well as concrete methods. 

                                                     Abstraction using public specifiers in OOPS
#include <iostream>

using namespace std;

// Abstract class with a pure virtual function

class Shape {

public:

    // Pure virtual function (abstract method)

    virtual void draw() = 0;

    // Concrete method

    void displayArea() {

        cout << “Area calculation is specific to each shape.” << endl;

    }

};

// Concrete subclass inheriting from the abstract class

class Circle : public Shape {

public:

    // Implementation of the pure virtual function

    void draw() override {

        cout << “Drawing a circle.” << endl;

    }

};

full stack web development
full stack web development

Abstraction In Oops Using Header Files

Header files in many programs are one of the best examples of understanding abstraction. Suppose there is a function sqrt () in the math.h header file. We will call the sqrt function whenever we need to calculate the square root of a number. 

However, we do not worry about how the sqrt() function calculates the square root of the number. This is where abstraction plays its part. It helps to save a lot of time and promotes reusability. 

Recommended Course

Abstraction In Oops Using Classes And Access Specifiers 

Classes organise different attributes and methods inside a frame and decide when to call a particular method. There are two main classifiers in Classes, which help in abstraction. 

1. Abstraction Using Public Specifiers

We can implement abstraction in OOPS using access specifiers. The public specifier is open to every user. They can access it from anywhere. The public specifiers are defined using the ‘public:’ keyword. The class elements can be accessed from outside the class when public access specifiers are used.

2. Abstraction using Private Classifiers 

The use of private classifiers restricts the class elements from accessing the private members without special permission. The private specifier is used to implement abstraction using class. The members, which are to be kept hidden, are enclosed inside the private access specifiers.

                                                  Abstraction using private classifiers in OOPS
#include <iostream>using namespace std;

class BankAccount {

private:

    // Private data members

    string accountNumber;

    double balance;

public:

  

    void setAccountDetails(string accNumber, double initialBalance) {

        accountNumber = accNumber;

        balance = initialBalance;

    }

    void deposit(double amount) {

        balance += amount;

        cout << “Deposit successful. Current balance: $” << balance << endl;

    }

    void withdraw(double amount) {

        if (amount <= balance) {

            balance -= amount;

            cout << “Withdrawal successful. Current balance: $” << 

            balance << endl;

        } else {

            cout << “Insufficient funds. Withdrawal failed.” << endl;

        }

    }

};

Recommended Reads

Data Science Interview Questions and Answers

Data Science Internship Programs 

Master in Data Science

IIT Madras Data Science Course 

BSC Data Science Syllabus 

Advantages of Abstraction in OOPs

Abstraction is used to present the necessary information to our users and simplify their interaction with our application. However, there are more benefits to abstraction. Let us know a few of them below.

  • The main advantage of abstraction is the reusability of code and the elimination of redundancy and duplication.
  • One can group many related classes using abstraction.
  • Abstraction in OOPs helps users avoid writing low-level code.
  • The user does not need to worry about the internal implementation of the application.
  • It helps reduce the complexity of interacting with the system and also increases its readability. 
  • It helps developers increase their development speed and ease.
Recommended Reads
Java Vs C++ Vs Python Java or C++ which is better
What is STL in C++ Top Features of C++ programming language

Abstraction in OOPs FAQs

Q1. What is an abstraction in Object-oriented programming?

Ans: Abstraction in OOPS means hiding unnecessary information and displaying only necessary information. It is very important to represent real-world objects in a simplified manner for the users to interact easily.

Q2. How many types of abstraction are there in object-oriented programming?

Ans: There are mainly two types of abstraction in object-oriented programming

1. Data abstraction

2. Process abstraction

Q3. How can we implement abstraction using access specifiers?

Ans: There are two major types of access specifiers that are used to implement abstraction in OOPS. Public specifiers can be accessed easily by anyone. However, private access specifiers cannot be accessed by other class members from outside. Read the complete article to learn more.

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe
Scroll to Top
close
counselling
Want to Enrol in PW Skills Courses
Connect with our experts to get a free counselling & get all your doubt cleared.