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 object and expose only necessary methods and properties through the reference of object.

 

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

 

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

 

Following is the example of defining a class with required methodsproperties and exposing it by 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 above code, we defined a Laptop class with required fieldsproperties and methods with public, private access modifiers to achieve an abstraction functionality by hiding and exposing some of methods and properties based on our requirements.

 

Here, the public modifier is used to allow a defined fieldsproperties and methods to access outside of the class and the private modifier is used to hide or restrict an access of required fieldsproperties and methods from the outside of class.

 

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

 

C# Abstraction Example Representation Diagram

 

If you observe above diagram, we are exposing 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 few properties and methods to access outside of the class in 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

When we execute above c# program, we will get the result like as shown below.

 

C# Abstraction Example Result

Difference between Abstraction and Encapsulation

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

 

AbstractionEncapsulation
It is used to hide unwanted data and show only required properties and methods. It is used to bind data members and member functions into single unit to prevent outsiders to access it directly.

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