Abstraction in C#

The characteristic of data abstraction is that it presents the user with only the most important details. The consumer is not presented with unnecessary or insignificant elements.

Another way to describe data abstraction is to focus on an object's necessary features while ignoring its extraneous aspects. An object's characteristics set it apart from objects of the same kind and aid in object classification and categorization.

Programmatically speaking, abstraction is the method of only displaying the pertinent aspects of an object's property to the user while concealing the feature's implementation details from view. You can concentrate on the object's functions rather than how it functions when abstracted. In C#, this is accomplished via interfaces, classes with abstract methods, and other mechanisms.

  • Classes Abstinent:

A class with abstraction and concrete members is known as an abstract class, as it cannot be instantiated independently. Derived classes are required to implement methods defined in an abstract class.

  • Interfaces:

An interface is an agreement that outlines the abstract methods that classes that implement it must have. In C#, interfaces allow for multiple inheritance.

Example: 

Assume you are taking out cash from an ATM in the real world. The user simply understands that when using an ATM, they must first insert their card, then input their card's PIN, enter the sum that they wish to withdraw, and finally, they will receive their money. The user has no idea how the ATM operates internally or how to take out money, among other things. The term abstraction refers to the user's basic understanding of what it takes to use the ATM.

Abstract Classes:

  • The abstract keyword is used to declare an abstract class.
  • You cannot construct instances of the abstract class in C#. Otherwise, you cannot utilize the new operator directly with the abstract class.
  • An abstract base class is a class that has the abstract keyword and some of its methods (but not all of them are abstract).
  • A clean Abstract Base Class has every method associated with the abstract keyword.
  • Declaring abstract methods outside of the abstract class is prohibited.
  • Declaring an abstract class as a sealed class is prohibited.
  • Declaring an abstract class as a static class is prohibited.

Using an example, abstract classes and abstract methods

In certain cases, we would like to develop a superclass that provides the general abstraction framework without going so far as to fully implement each method. To put it another way, there are situations where we wish to design a superclass that just specifies an umbrella structure that all of its subclasses will share, leaving the specifics up for every subclass to figure out.

Think of a traditional "shape" example, such as one from a gaming simulation or computer-aided design system. "Shape" is the base type; every form has a color, size, and other attributes. This results in the derivation (inherited) of particular shapes, such as triangles, squares, circles, and so forth, each may have extra traits and behaviors. For instance, it is possible to flip some shapes. Certain behaviours—like attempting to compute the size of a square—might differ.

Example Code:

using System;

namespace Demoabstraction {

abstract class Shape {

            public abstract int area();

}

class Square : Shape {

            private int side;

            public Square(int p = 0)

            {

                        side = p;

            }

            public override int area()

            {

                        Console.Write("Area of Square: ");

                        return (side * side);

            }

}

class GFG {

            static void Main(string[] args)

            {

                        Shape sh = new Square(5);

                        double result = sh.area();

                        Console.Write("{0}", result);

            }

}

}

Output:

Abstraction in C#

main advantages of abstraction in c#

  • Hides Implementation Details:

Abstraction lets you provide only the information required for external interaction with a class or module, keeping the internal workings hidden. As a result, using and understanding the code is made simpler and requires less attention to underlying complexity.

  • Encapsulation:

Encapsulation and abstraction are closely related concepts. It enables you to isolate a class's internal operations and create a distinct division between the implementation—which handles how a class operates—and the interface, which determines what is visible. The sophistication of big software systems can be better managed as a result.

  • Code Reusability:

You can specify common characteristics that apply to different classes by using abstract classes as well as interfaces. This encourages code reuse and aids in the creation of flexible, sustainable applications.

  • Polymorphism:

In the case of object-oriented programming, polymorphism is made possible by the fundamental idea of abstraction. Entities of various kinds can be objects of the same base type because of polymorphism, which gives designs flexibility and adaptability.

Code maintenance is made easier by abstraction, which allows modifications to a class's internal implementation without affecting the external code that utilizes it. Because of this separation, changes have less impact, and the code can be maintained and changed more easily over time.

  • Flexibility and Extensibility:

 Systems built with abstraction are both adaptable and expandable. A scalable and scalable structure can be promoted by adding new classes inherited from the same abstract class or implementing the same interface without changing the existing code.