Data Abstraction is the property by virtue of which only the essential details are exhibited to the user. The trivial or the non-essentials units aren’t exhibited to the user.
Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. The properties and behaviors of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects.
Example: Consider a real-life scenario of withdrawing money from ATM. The user only knows that in ATM machine first enter ATM card, then enter the pin code of ATM card, and then enter the amount which he/she wants to withdraw and at last, he/she gets their money. The user does not know about the inner mechanism of the ATM or the implementation of withdrawing money etc. The user just simply know how to operate the ATM machine, this is called abstraction.
In C# abstraction is achieved with the help of Abstract classes.
- An abstract class is declared with the help of abstract keyword.
- In C#, you are not allowed to create objects of the abstract class. Or in other words, you cannot use the abstract class directly with the new operator.
- Class that contains the abstract keyword with some of its methods(not all abstract method) is known as an Abstract Base Class.
- Class that contains the abstract keyword with all of its methods is known as pure Abstract Base Class.
- You are not allowed to declare the abstract methods outside the abstract class.
- You are not allowed to declare abstract class as Sealed Class.
Using abstract classes and abstract methods with an example
There are situations in which we want to define a superclass that declares the structure of a given abstraction without providing a complete implementation of every method. That is, sometimes we want to create a superclass that only defines a generalized form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details.
Consider a classic “shape” example, perhaps used in a computer-aided design system or game simulation. The base type is “shape” and each shape has a color, size and so on. From this, specific types of shapes are derived(inherited)-circle, square, triangle and so on – each of which may have additional characteristics and behaviors. For example, certain shapes can be flipped. Some behaviors may be different, such as when you want to calculate the area of a square.
Area of Square: 16
Encapsulation vs Data Abstraction
- Encapsulation is data hiding(information hiding) while Abstraction is detail hiding(implementation hiding).
- While encapsulation groups together data and methods that act upon the data, data abstraction deals with exposing to the user and hiding the details of implementation.
Advantages of Abstraction
- It reduces the complexity of viewing the things.
- Avoids code duplication and increases reusability.
- Helps to increase security of an application or program as only important details are provided to the user.
- Difference between Python and C#
- Difference between C and C#
- Writing to Excel Sheet Using EPPlus in C#
- Lambda Expressions in C#
- How to Get Started with Game Development?
- Basics of File Handling in C#
- C# | ToolTip Class
- C# | RichTextBox Class
- C# | MaskedTextBox Class
- C# | NumericUpDown Class
- C# | DateTimePicker Class
- C# | GroupBox Class
- C# | ListBox Class
- Different ways to convert String to Integer in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.