![]() |
VOOZH | about |
Abstraction is the property by which only the essential details are shown to the user and non-essential details or implementations are hidden from the user.
Example: Consider a real-life scenario of withdrawing money from an ATM. The user only knows that in the ATM machine first enters the ATM card, then enters the PIN code of the ATM card, and then enters the amount which the user wants to withdraw and at last, they get their money. They don't know about the inner mechanism of the ATM or the implementation of withdrawing money etc. The user just simply knows how to operate the ATM machine, this is called abstraction.
In C# abstraction is achieved with the help of Abstract classes and Interfaces. Both of them are discussed below:
An abstract class is declared with the help of an abstract keyword. In C#, it is not allowed to create objects of an abstract class. We create a derived class take properties of the abstract class and use them to achieve abstraction. Furthermore, we can access it from the Instance of the derived class object.
Example:
Area of Square: 16
The interface is declared using the interface keyword. We can create abstract methods inside the interface similar to abstract classes and declare their functionality in the derived class. From the derived class we can access those properties using the derived class by creating the Instance of the derived class. Example:
My favorite subject is C#
Abstraction and encapsulation are the important features of OOPs. Their functionality is similar and has slight differences which are shown below.
Features | Abstraction | Encapsulation |
|---|---|---|
Definition | Hiding the implementation and exposing the essential details only. | Encapsulate the data and information in a single unit and provide access through accessors. |
Implementation | Implements using abstract classes and interface and properties | Implemented using access modifiers in C# |
Purpose | Used to hide the complex functionality and show the user-friendly Interface | Provide security and prevent unauthorized data access. |
Example | A car class has different features like drive(), brake () and gear() and we can use it without knowing the implementation. | Get () and set() accessors to provide access controls on data. |