Introduction to abstraction for beginners
Tags: beginners, oop, csharp
Published: 2022-05-12 03:17:39

In object-oriented programming, abstraction is a critical concept that allows you to create classes and methods that represent the general concept of an object rather than specific instances of that object. The purpose of this is to hide implementation details from users. This blog will explain what abstraction is, how it is implemented in C# and the advantages of abstraction. 

## **What is abstraction in object-orientated programming? **
Abstraction in object-orientated programming hides unimportant and complex details from the user and only displays the vital information. This means a user doesn't need to understand the complexities of a program in order to use it. When you drive a car, you don't need to know how the engine works to get from point A to point B. You can get in the car and turn the key, and the engine will take care of the rest. This is an example of abstraction in the real world. 

Abstraction is often confused with encapsulation. However, encapsulation and abstraction are two different concepts. Encapsulation is another key concept in object-oriented programming that refers to bundling data with the methods that operate on that data and involves restricting access to the data and methods, which is sometimes referred to as data hiding. Abstraction is a way of hiding the implementation details of an object from the user, whereas encapsulation is a way of hiding the data itself from the user. 
**How do abstract classes provide abstraction?**
An abstract class is a class that cannot be instantiated, which means that you cannot create an object of the abstract class type. An abstract class can, however, be inherited by another class. When an abstract class is inherited, the inheriting class must provide implementations for all of the abstract members of the base class. The result is that the base class hides the implementation details of those members from the inheriting class by effectively adding a layer ( the abstract class) of abstraction. 

## **How to implement abstraction in C#**
As mentioned previously, abstraction is implemented in classes and methods. In C#, you can create an abstract class using the keyword "abstract" and it can contain abstract and non-abstract members. An abstract class is a class that cannot be instantiated, meaning you cannot create an object of that type. Abstract methods do not have a body, and they must be implemented in a derived class. The non-abstract methods are methods that have a body, and they can be called without having to implement them in a derived class. In the following example, we have created an abstract class called "Animal" with one abstract method and one non-abstract method:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kh5g7q32osvr3bw5hdyh.png)
 
As you can see, the "Animal" class has an abstract method called "MakeNoise" and a non-abstract method called "Sleep". The "MakeNoise" method does not have a body, so it must be implemented in a derived class. The "Sleep" method has a body, so it can be called without having to implement it in a derived class.
You cannot create an abstract method in a non-abstract class. In the following example, we have attempted to create an abstract method called "MakeNoise" in the "Animal" class:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1ydo8qheffunt3oowqa.png)

As you can see, the "Animal" class is showing  the abstract method called "MakeNoise" with an error.
 
When you create an abstract method, you must provide a body for the method in a derived class. You can do this by using the keyword "override ". In the following example, we have created a derived class called "Dog" that overrides the "MakeNoise" method:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oidq404ihwx5i7km89ot.png)

As you can see, the "Dog" class overrides the "MakeNoise" method by providing a body for the method. When you call the "MakeNoise" method on an object of type "Dog", the "Woof!" message will be displayed.

## What are the advantages of abstraction? 
There are several benefits of abstraction in software development:
- Abstraction allows you to create reusable code.
- Abstraction makes your code more efficient and easier to maintain.
- Abstraction can make your code more flexible and extensible.
- Abstraction can help you prevent code duplication.
- Abstraction can make your code more understandable and easier to use.

There you have it, an introduction to abstraction! I am writing this blog as a student in Object-Oriented development and I hope you find it helpful! If you have any questions, please feel free to leave a comment below. Thanks for reading!