Abstraction in Object Oriented Programming (OOPS)

In philosophical terms, Abstraction is a process of understanding behaviour or structure of a real-life object. For example, when we think about a car, we remember an abstract concept of a car and its functionalities. This is why we can recognize object like car, even if it is different from any car we have seen before. In other words, we develop concepts of everyday objects through the process of abstraction, where we eliminate unnecessary details and focus on essential behaviour.

Real life example of abstraction in object oriented programming

Abstraction is also a common feature in real life applications. For example:

  • When we login to an online account, we enter user id and password and click login button. The process of how input data is sent to the server and verified is abstracted from us.
  • When we type on a computer, we press keys to produce results on the screen. Here we don't need to know mechanism behind how output is produced.
  • In Java, HashMap class provides methods for storing and retrieving key-value pairs. All we need to know to the use cases of HashMap in our application. The implementation details are abstracted from us.

What is abstraction in OOPS?

Object-Oriented Programming uses abstraction to separate the interface of an object from its internal implementation. It defines external behavior of an object and encapsulates its internal workings. This allows developers to interact with objects based on their intended behavior, without understanding the details of how the behavior is achieved.

Abstraction in oops example

In other words, abstraction in OOPS enables the hiding of the internal details of an object from the outside world, so that the focus is on what the object does, rather than how it does it.

Critical ideas to think!

  • An object has attributes and methods. By using access modifiers, we can hide important details and provide access to necessary methods and attributes to the outside world.
  • Abstraction help us define objects as abstract "actors" that can perform set of operations, change their state and communicate with other objects.
  • We can also understand abstraction through inheritance. In inheritance hierarchy, parent classes contain general implementation, while child classes contain more specific implementation.
  • Abstraction is a key concept behind object-oriented principles like Open-Closed Principle and Dependency Inversion Principle.

How do we implement abstraction in Java?

In Java, we implement abstraction using abstract classes and interfaces.

Abstraction in Java using abstract classes

An abstract class is declared with the abstract keyword and can contain both abstract methods (methods without body) and non-abstract methods. Abstract classes cannot be instantiated, but can be subclassed.

To implement abstraction using abstract classes:

  • We declare a class as an abstract class using abstract keyword. This will work as a parent class for the concrete subclasses.
  • Inside the abstract class, we declare abstract methods and provide default implementation for the non-abstract methods.
  • We create concrete subclasses by extending abstract class as a parent class. Inside concrete subclasses, we provide implementation of abstract methods defined in the abstract class.

Example Java code

abstract class Shape {
   abstract void draw();

   void fillColor(String color) {
      System.out.println("Filling color " + color + " for shape");
   }
}

class Circle extends Shape {
   void draw() {
      System.out.println("Drawing Circle");
   }
}

class Driver {
   public static void main(String[] args) {
      Shape shape = new Circle();
      shape.draw();
      shape.fillColor("Red");
   }
}

Explore this blog for more details: Abstract Class in Java

Abstraction in Java using interfaces

In Java, we can also achieve abstraction using interfaces, which serve as blueprints for classes and define methods that must be implemented. Interfaces are reference types that consist of constants, method signatures, default methods and static methods.

  • Only default methods and static methods have method bodies.
  • Interfaces cannot be instantiated but can be implemented by classes or extended by other interfaces.

Example Java code

public interface Shape {
   int SIDES = 4;

   void draw();

   default void fillColor(String color) {
      System.out.println("Filling color " + color + " for shape");
   }

   static void printSides() {
      System.out.println("Number of sides: " + SIDES);
   }
}

class Square implements Shape {
   public void draw() {
      System.out.println("Drawing Square");
   }
}

class Driver {
   public static void main(String[] args) {
      Shape shape = new Square();
      shape.draw();
      shape.fillColor("Red");
      Shape.printSides();
   }
}

Explore this blog for more details: Interface in Java

How do we implement abstraction in C++?

In C++, abstraction is implemented using access specifiers and abstract classes.

Abstraction in C++ using abstract classes

An abstract class in C++ is a base class that cannot be instantiated. It contains at least one pure virtual function, which is declared using the "virtual" keyword and the "= 0" notation in the function declaration. The purpose of an abstract class is to provide a common interface for its derived classes.

  • A derived class must implement pure virtual function(s) declared in the abstract base class, otherwise it will also become an abstract class.
  • The derived class can have its own implementation of the function.

Example C++ code

class Shape {
 public:
    virtual void Draw() = 0;
};

class Circle : public Shape {
 public:
    void Draw() { cout << "Drawing Circle" << endl; }
};

class Square : public Shape {
 public:
    void Draw() { cout << "Drawing Square" << endl; }
};

int main() {
    Shape *shape = new Circle();
    shape->Draw();
    shape = new Square();
    shape->Draw();
    return 0;
}

In summary, an abstract class is a blueprint for its derived classes, providing a common interface and enforcing the implementation of certain functions. We will discuss abstraction in C++ in a separate blog later.

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: Data abstraction and Control abstraction.

Data Abstraction

When object data is hidden from the outside world, it creates data abstraction. If required, access to that data is provided through public methods, which are called getters and setters. These methods act as an interface between the object and the external world.

Data abstraction provides a clear separation between the internal implementation and external usage of an object. It helps developers change the internal representation of an object without affecting the code that interacts with it. So, data abstraction promotes code modularity and improves code maintainability.

class DataAbstraction {
    private int data;

    public DataAbstraction(int data) {
        this.data = data;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
    
    // Other private or public methods working on data
}

Control Abstraction

In object-oriented programming, control abstraction is the abstraction of actions. Often, 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.

interface Bank {
    double getBalance();
    void deposit(double amount);
    void withdraw(double amount);
}

class SavingsAccount implements Bank {
    private double balance;
    
    public SavingsAccount(double balance) {
        this.balance = balance;
    }
    
    @Override
    public double getBalance() {
        return balance;
    }
    
    @Override
    public void deposit(double amount) {
        balance = updateBalance(balance, amount, true);
    }
    
    @Override
    public void withdraw(double amount) {
        balance = updateBalance(balance, amount, false);
    }
    
    private double updateBalance(double balance, double amount, boolean deposit) {
        if (deposit) {
            return balance + amount;
        } else {
            return balance - amount;
        }
    }
}

class Driver {
    public static void main(String[] args) {
        Bank account = new SavingsAccount(1000.0);
        System.out.println("Initial balance: " + account.getBalance());
        
        account.deposit(500.0);
        System.out.println("Balance after deposit: " + account.getBalance());
        
        account.withdraw(200.0);
        System.out.println("Balance after withdrawal: " + account.getBalance());
    }
}

In the above example, client code can create an instance of the SavingsAccount class and perform banking operations without having to know how operations are done internally. The updateBalance method is a private method that performs actual update of the balance and is only accessible within the class. This creates control abstraction.

Note: Control abstraction is a primary purpose of programming languages. At the machine level, computers operate on low-level instructions, like moving bits between memory locations or adding two sequences of bits. Programming languages provide a higher level of abstraction, letting programmers express these operations in simple terms without managing the underlying machine-level details.

Abstraction vs. Encapsulation

A well-designed code with proper 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”. 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 comes into the picture: encapsulation!

Difference between Abstraction and Encapsulation

Abstraction vs. Encapsulation Comparison

Advantages of Abstraction

  • Simplifies code and reduces complexity: Abstraction create a boundary between the application and the client code, which can help reduce the complexity of software.
  • Facilitates modular and flexible design: Abstraction helps us divide responsibilities into software entities (classes, methods, etc.), where user only know necessary functionalities, but not how that functionality is implemented.
  • Improves code readability, reusability and maintainability: Abstraction maximises the ease of use of relevant information. In other words, code with proper abstraction helps programmers quickly understand what the code does and how it is meant to be used.
  • Abstraction allows programmers to simplify programming and shift focus from implementation details and avoid writing low-level code. On other side, It also allows programmers to change the internal implementation of methods or concrete classes without affecting the interface.

Conclusion

In object oriented programming, Abstraction is the practice of only exposing the necessary details to the client or user. This means that when a client uses a class, they don't need to know the inner workings of the class's operations. This decouples the user of the object from its implementation, making it easier to understand and maintain. If there is a change in an operation, only the inner details of the related method need to be updated.

Enjoy learning, Enjoy OOPS!

Find us on:

Self-paced Courses and Blogs