Let’s understand what is Abstraction in OOP

Sriram Kumar Mannava
2 min readApr 15, 2023

What is Abstraction?

Abstraction can be simply stated as exposing only which is required for the context.

It means to only provide access to those features or contents which are needed for the moment.

The client or the requesting component need not know all the details of the component it is requesting, it just needs to be informed of only things it requires to get things done.

For example…

Abstraction is a real-world principle which applies to all state machines and applications we use in our daily life.

For example, we may generally use a music player to access and play music from some music source (local storage, spotify or some other streaming service).

A music player generally provides options to search, play, pause or stop playing. We use these options to apply our desired operation.

But nobody knows what happens when one presses a play button or pause button. It is irrelevant for the purpose of his using the music player. Knowing what happens internally is completely out of context for a normal user.

How to abstractions are created in OOP?

In a programming language perspective, abstraction refers to the logic to be exposed to the requesting component.

We provide access to the data or the processing functionality to a requesting component via abstractions, i.e. which just expose what is required but not everything.

We call these as abstract classes. In any programming language, we have abstract classes and interfaces to provide abstractions to other components.

An interface serves as a blueprint or a menu card for the component at the requesting component end, so that the components on the outside can never know which derivative they’re accessing in the name of this face.

For example, a MusicPlayer interface can look like below -

using System.Collections.Generic;

namespace MyMusicPlayer.Contracts.Abstractions
{
public interface IMusicPlayer
{
void play(string songName);
void pause();
void stop();
IEnumerable<string> search(string keyword);
string resume();
}
}

Types of Abstractions

We can apply abstraction at the feature level or at the data level to restrict access.

Data Abstraction — The songs list in the MusicPlayer is not accessible to the outside, the other components don’t even know that there’s a list called playQueue which is being used to add or remove the songs. They can only access via the functionalities.

Feature Abstraction — The songsLibrary collection in the LibraryService is being fetched up from a non-descriptive source which is a functionality unknown at this class level.

--

--

Sriram Kumar Mannava
Sriram Kumar Mannava

Written by Sriram Kumar Mannava

I make Full Stack Development Easy for You | Full Stack .NET Dev | 3× AWS Certified | Blogger

No responses yet