This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

Abstraction


In this article we are going to discuss Abstraction in C#. There are many blogs, articles are available on the internet regarding Abstraction but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Abstraction use in C#.

Abstraction Introduction

  1. Abstract classes are the way to achieve abstraction in C#. Abstraction in C# is the process to hide the internal details and showing functionality only.

    Abstraction can be achieved by two ways:

    1. Abstract class
    2. Interface

  2. An abstract class is declared with the help of abstract keyword.
  3. 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.
  4. In C#, abstract class is a class which is declared abstract. It can have abstract and non-abstract methods.
  5. You are not allowed to declare the abstract methods outside the abstract class.
  6. You are not allowed to declare an abstract class as Sealed Class.
  7. You are not allowed to declare an abstract class as Static Class.

The abstract keyword is used for classes and methods.

  1. Abstract class - It is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
  2. Abstract method - It can only be used in an abstract class, and it does not have a body. The body is provided by the derived class (inherited from).

Real World Example of Abstraction

Example1

Let's consider a real-life example - a man driving a car. The man knows what the steering wheel does, but he doesn't know how these things are done internally by the car. He doesn't know about the inner mechanisms such as how internally engine works. This is known as abstraction is.

Abstraction

Example2

Considering a real-life scenario of withdrawing money from an ATM, an example of abstraction can be given here:

A person who withdraws money from an ATM only knows that in the ATM machine, they need to first insert the ATM card, then enter the PIN code of the ATM card, then check the balance of the account they want to withdraw from, and finally, they receive the withdrawn amount. They have no knowledge of the internal workings of the ATM or the process of withdrawing money. This is an excellent example of abstraction.

Example3

when we log in to any social networking site like Facebook, Twitter, LinkedIn, etc., we enter our user ID and password, and then we get logged in. Here, we don’t know how they are processing the data or what logic or algorithm they are using for login. This information is abstracted/hidden from us since they are not essential to us. This is basically what abstraction is.

Example1

In this example, We have implemented above examle through code.

using System;

// Abstract class representing an ATMMachine
public abstract class ATMMachine
{
    // Abstract method to Withdraw Amount
    public abstract void WithdrawAmount(int amount);
}

// Concrete implementation of ATM
public class ConcreteATM : ATMMachine
{
    // Implementation of withdrawing Amount
    public override void WithdrawAmount(int amount)
    {
        Console.WriteLine($"Withdrawing ${amount} from the ATM...");
        // Code to actually withdraw Amount would go here
        Console.WriteLine("Given Amount withdrawn successfully from ATM Machine!");
    }
}

// Client class representing a user interacting with the ATMMachine
public class User
{
    // Method for user interaction
    public void UseATMMachine(ATMMachine atm, int amount)
    {
        // User interacts with the ATMMachine without knowing its internal workings
        atm.WithdrawAmount(amount);
    }
}
class Program
{
    static void Main(string[] args)
    {
        // Creating objects
        ConcreteATM atm = new ConcreteATM();
        User user = new User();

        // User interaction with ATMMachine
        user.UseATMMachine(atm, 400); // User wants to withdraw amount $400
    }
}

In the Main method, we create instances of ConcreteATM and User and demonstrate the user's interaction with the ATMMachine without knowledge of its internal workings.

Output

Abstraction

Example2

If you observe the below code, we defined a Laptop class with required fields, properties, 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.

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 LaptopCompanyDetails()
        {
            Console.WriteLine("Brand: " + Brand);
            Console.WriteLine("Model: " + Model);
        }
        public void LaptopKeyboard()
        {
            Console.WriteLine("Type using Keyword");
        }
        private void MotherBoardInfo()
        {
            Console.WriteLine("MotheBoard Information");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Laptop objLap = new Laptop();
            objLap.Brand = "Lenovo";
            objLap.Model = "Model-14";
            objLap.LaptopCompanyDetails();
            objLap.LaptopKeyboard();
        }
    }
}

Output

In the above example we define some fields private so we can not access these field directly so we have used properties for it. so in that case private field will be hide. We have created some method which no need to show. It makes private. 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.

Abstraction
Next