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.
Table of Contents
- What is Abstraction?
- Unlimited ways to write code!!
- The Purpose of Abstraction
- How Does an Abstraction Look in Code (C#)
- Abstraction Hierarchy
- Other Benefits of Abstraction
What is Abstraction?
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.
Code Sample - OOPS Abstraction Unlimited Ways to Write 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
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 allows us to express complex ideas as easily as simple ones. The idea is to break down the complex problem into simple methods.
Code Sample - OOPS Abstraction Hierarchy
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(" ", "-"),
Substring(0, 50). Higher abstraction depends on lower abstractions.
NormalizeStudentName - Higher-order abstractions
Code Sample - OOPS Abstraction Hierarchy
Demo - Abstracted Email Address
Scenario - Let's try creating a email address that is properly abstracted from
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 amoun of bugs. Thus helps to focus on one task at a time and focus on WHAT's, not HOW's.
Even higher-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.
Code Sample - OOPS Abstraction Benefits
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.