Abstraction implementation in C#

 In object-oriented programming, Abstraction is the process of hiding certain details and showing only essential information to the user.

Why Abstraction is needed?

Abstraction allows us to create a general idea of what the problem is and how to solve it. The process instructs us to remove all specific detail and any patterns that will not help us solve our problem.

In C#, Abstraction can be achieved by using Abstract Class(es) and Interface(s).

Let's take an example to implement this with C#:


In the preceding example, we have achieved abstraction with the help of the IDevice interface, which is implemented by the Android, IPhone, and Window concrete classes. So, to keep abstraction between concrete implementation and users, we used a DeviceManager class, which is responsible to provide the instance of the specific device based on provided device type. In the DeviceManager class,  the GetDevice() method returns an instance of the IDevice interface type instead of concrete classes (Android, IPhone, and Window). 

Here, the users don't know about concrete implementation, they know about the problem and how to solve it.

Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form