C# Abstraction with Examples

In c#, Abstraction is a principle of object-oriented programming language (OOP), and it is used to hide the implementation details and display only essential features of the object.

 

In Abstraction, by using access modifiers, we can hide the required details of the object and expose only necessary methods and properties through an object's reference.

 

In real-time, the laptop is a perfect example of abstraction in c#. A laptop consists of many things such as a processor, RAM, motherboard, LCD screen, camera, USB ports, battery, speakers, etc. To use it, we need to know how to operate the laptop by switching it on, and we don’t need to know how internally all the parts are working. Here, the laptop is an object designed to expose only required features by hiding its implementation details.

 

In object-oriented programming, a class is the perfect example of abstraction. In c#, we can create a class with required methods, and properties and we can expose only necessary methods and properties using access modifiers based on our requirements.

 

The following is an example of defining a class with required methods and properties and exposing it using access modifiers to achieve abstraction functionality.

 

public class Laptop
{
   private string brand;
   private string model;
   public string Brand
   {
     get { return brand; }
     set { brand = value; }
   }
   public string Model
   {
      get { return model; }
      set { model = value; }
   }
   public void LaptopDetails()
   {
      Console.WriteLine("Brand: " + Brand);
      Console.WriteLine("Model: " + Model);
   }
   public void LaptopKeyboard()
   {
      Console.WriteLine("Type using Keyword");
   }
   private void MotherBoardInfo()
   {
      Console.WriteLine("MotheBoard Information");
   }
   private void InternalProcessor()
   {
      Console.WriteLine("Processor Information");
   }
}

If you observe the above code, we defined a Laptop class with required fieldsproperties, and methods with the public, and private access modifiers to achieve an abstraction functionality by hiding and exposing some of the methods and properties based on our requirements.

 

Here, the public modifier allows defined fields, properties, and methods to access outside of the class. The private modifier is used to hide or restrict access to required fieldsproperties, and methods outside of class.

 

By creating an instance of the Laptop class, we can access defined fieldsproperties, and methods. Following is the pictorial representation of creating an instance of a class and accessing fieldsproperties, and methods.

 

C# Abstraction Example Representation Diagram

 

If you observe the above diagram, we expose only the necessary properties and methods outside of the class by using public and private access modifiers.

C# Abstraction Example

Following is the example of implementing an abstraction functionality by allowing only a few properties and methods to access outside of the class in the c# programming language.

 

using System;
using System.Text;

namespace Tutlane
{
    public class Laptop
    {
       private string brand;
       private string model;
       public string Brand
       {
          get { return brand; }
          set { brand = value; }
       }
       public string Model
       {
          get { return model; }
          set { model = value; }
       }
       public void LaptopDetails()
       {
          Console.WriteLine("Brand: " + Brand);
          Console.WriteLine("Model: " + Model);
       }
       public void LaptopKeyboard()
       {
          Console.WriteLine("Type using Keyword");
       }
       private void MotherBoardInfo()
       {
          Console.WriteLine("MotheBoard Information");
       }
       private void InternalProcessor()
       {
          Console.WriteLine("Processor Information");
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
          Laptop l = new Laptop();
          l.Brand = "Dell";
          l.Model = "Inspiron 14R";
          l.LaptopDetails();
          Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}

If you observe the above example, we defined fields, properties, and methods with the public, and private access modifiers to allow or disallow properties, and methods access based on requirements.

 

When you execute the above c# program, you will get the result below.

 

C# Abstraction Example Result

Difference between Abstraction and Encapsulation

The following are the differences between abstraction and encapsulation in the c# programming language.

 

AbstractionEncapsulation
It is used to hide unwanted data and shows only the required properties and methods. It binds data members and member functions into a single unit to prevent outsiders from accessing it directly.

This is how we can use abstraction to hide implementation details and show only essential features based on our requirements in the c# programing language.