
OOPS Abstraction
Author - Abdul Rahman (Bhai)
OOPS
2 Articles
Table of Contents
What we gonna do?
In our previous post we saw what is Encapsulation. I strongly recommend you to go through previous article if you didn't read it. Encapsulation and Abstraction are highly interconnected but still have a different principle. In this post lets see what is Abstraction.
Why we gonna do?
When we write code, we have unlimited ways to approach any problem. This freedom can become a liability. Without abstraction, developers tend to write everything in one place — giant methods, monolithic classes, tangled logic. Our brains are limited and can only process a small number of things at once. When code exceeds that capacity, development slows and bugs multiply.
Abstraction gives us a tool to manage this cognitive overload. By separating concerns and hiding implementation details behind well-named abstractions, we can write code that communicates WHAT it does without forcing you to understand HOW it works. Without this discipline, every change becomes an archaeological dig through irrelevant details.
That is why understanding abstraction is foundational to writing good software. It is not just an academic concept but a practical skill that directly impacts how fast you can build, debug, and maintain a system.
How we gonna do?
What is Abstraction?
Abstraction is the amplification of the essential and the elimination of irrelevant.
Unlimited ways to write code!!
When we write code we have unlimited options to write code. We can write code in different ways for any task we think of. There are no restrictions. For example, we can write entire application in one class but that will be terrible idea and the reason is our brains are limited and can only handle small things at a time. If this exceeds then our development slows down and we start to introduce lots of bugs.
public class Program
{
public void MySuperBigMethod()
{
// 100,000,000 lines of code
}
}
public class MySuperBigMethod
{
// 100,000,000 lines of code
}
So its better to avoid freestyle mode and thats why all guidelines and structure for programming exists.
The Purpose of Abstraction
Abstraction helps you focus on one particular task. That is,
- Amplification of the essential = The current task.
- Elimination of the irrelevant = All other tasks.
This way we can split our application into chunks and tackle them one by one.
How Does an Abstraction Look in Code (C#)?
abstract class MyClass ? or interface IMyInterface ? Abstraction doesn't have anything to do with keywords
abstract or interface. These are just language specific implementation
details. All code is a abstraction.
- Good Abstraction = Focusing on single application concern.
- Bad Abstraction = Thinking of multiple concerns.
Abstraction is also related to Single Responsibility Principle (SRP) (first principle of SOLID). If a code violates SRP then it is bad abstraction.
Abstraction Hierarchy
Abstraction allows us to express complex ideas as easily as simple ones. The idea is to break down the complex problem into simple methods.
// Before
string trimmedName = studentName.Replace(" ", "-").Trim();
if (trimmedName.Length > 50)
{
trimmedName = trimmedName.Substring(0, 50);
}
// Abstracting
private string NormalizeStudentName(string name)
{
string trimmedName = name.Replace(" ", "-").Trim();
if (trimmedName.Length > 50)
{
trimmedName = trimmedName.Substring(0, 50);
}
return trimmedName;
}
// After
string normalizedName = NormalizeStudentName(studentName);
Here to focus on single task, the trimming functionality is abstracted to new method so that this code doesn't needs to be repeated and called at every place. This new method is an abstraction. It amplifies the essential: WHAT (=NormalizeStudentName - High level detail) the code does and eliminates the irrelevant: HOW (=Method Implementation - Low level detail) the code does it. The code that uses this abstraction doesn't need to worry about how it is implemented.
Abstractions are built on top of other abstractions like Replace(" ", "-"), Trim() and Substring(0, 50). Higher abstraction depends on lower abstractions.
NormalizeStudentName - Higher-order abstractions
Lower-order abstractions
- Replace
- Trim
- Substring

public class EmailAddress
{
public string Value { get; }
private EmailAddress(string value)
{
Value = value;
}
public static EmailAddress Create(string address)
{
if (!Regex.IsMatch(address, @"^(.+)@(.+$)"))
throw new Exception();
return new EmailAddress(address);
}
}
This class represents Email Address and it has hierarchy of abstractions. It abstracts away the work with strings and creates its own higher order abstraction such the client code can focus on tests related to Email Address than with strings. As a result we get the below graph of abstractions. Thus allowing us to build complex logic as easily as simple ones. This will be within our brain capacity and doesn't slow down development and won't introduce large amount of bugs. Thus helps to focus on one task at a time and focus on WHAT's, not HOW's.
Even higher-order abstractions
Higher-order abstractions
Lower-order abstractions
Other Benefits of Abstraction
Abstraction is not just for code reuse. We should introduce this method even if it is used in one place. Thats because its easier to read the following code than to understand the implementation. It is also clear at highlevel and gives better code simplification.
string studentName = ReadStudentName();
string normalizedName = NormalizeStudentName(studentName);
SaveStudentName(normalizedName);
Summary
In this post, we learn't what is abstraction in oops and the principle behind it. It means amplification of the essential and elimination of the irrelevant. It helps to focus on,
- one task at a time
- on WHAT's (intent), not HOW's (implementation details)
Abstraction can form Hierarchies which helps express complex ideas as easily as simple ideas.