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
The abstract keyword is used for classes and methods.
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.
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
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.