C# | Abstraction

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.

Real world Example: 

Imagine a real-life scenario of withdrawing money from an ATM. The user only knows to first enter her ATM card at the ATM, then enter her PIN code of her ATM card, then enter the amount he wants to withdraw and finally receive the money. Users do not know anything about the internal workings of the ATM or the implementation of money withdrawals. The user only knows how to operate the ATM, and this is called abstraction.

In C# abstraction is achieved with the help of Abstract classes and Access modifiers

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 an abstract class as Sealed Class.
  • You are not allowed to declare an abstract class as Static Class.

Using abstract classes and abstract methods with an example

You may want to define a superclass that declares the structure of a particular abstraction without providing a complete implementation of each method. That is, you may want to create a superclass that just defines a generalized form that is shared by all subclasses, leaving the details to each subclass.

Consider the classic example of “shape”, perhaps used in computer-aided design systems or game simulations. The basic type is “shape”, and each shape has a color, size, etc. From this, certain types of shapes are derived (inherited), such as circles, squares, and triangles, each of which can have additional properties and behaviors.

For example, you can mirror a certain shape.
Some behavior may be different, for example when calculating the area of ​​a square.

Example: 

// C# program to calculate the area
// of a square using the concept of
// data abstraction
using System;
 
namespace Testabstraction
 {
     // abstract class
      abstract class Shape 
     {
        // abstract method
        public abstract int area();
     }
 
// square class inheriting
// the Shape class
class Square : Shape 
{
 
    // private data member
    private int side;
 
    // method of  square class
    public Square(int x = 0)
    {
        side = x;
    }
     
    // overriding of the abstract method of Shape
    // class using the override keyword
    public override int area()
    {
        Console.Write("Area of Square: ");
        return (side * side);
    }
}
 
// Driver Class
class CodeConfig 
{
     
    // Main Method
    static void Main(string[] args)
    {
        // creating reference of Shape class
        // which refer to Square class instance
        Shape sh = new Square(4);
         
        // calling the method
        double result = sh.area();
         
        Console.Write("{0}", result);
  
    }
}
}

Output: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 deal with exposing to the user and hiding the details of implementation.

Advantages of Abstraction

  • It reduces the complexity of viewing things.
  • Avoids code duplication and increases reusability.
  • Helps to increase the security of an application or program as only important details are provided to the user.

I hope you enjoyed reading this post. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such posts.

Leave a Reply

Your email address will not be published. Required fields are marked *