Show only the necessary details of an object is called abstraction. In other words capture only the related details of an object of current perspective is called abstraction.
Description: Supposed we want to
implement abstraction of the following object.
Object: Pizza.
For
customers’ perspective: They want to eat different flavor, choose various
quantity, receiving pizza and also can place order for pizza. So flavor,
quantity, receiving pizza and place order are current perspective of customer. So these methods and
properties will be public (Abstraction). And all other methods or properties
will be private because customers don’t need to know about it (how to making
pizza, how to packing pizza).
Advantage: The advantage of abstraction is to lessen the complexity of an object.
See the given example code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractionDemonstration
{
public class Pizza
{
public String Flavour { get;
set; }
public Int32 Quantity { get;
set; }
public void OrderPizza() //Customer
can order
{
Console.WriteLine("I want to order " + Quantity + " " + Flavour + "
pizza...");
}
public void ReceivingPizza() //Customer
is receiving pizza
{
Console.WriteLine("Here is your " + Quantity + " " + Flavour + "
pizza. Thanks for your order.");
}
private
void OrderReceived() //
Order received by Pizza Shop.
{
Console.WriteLine("Received order of " + Quantity + " " + Flavour + "
pizza.");
}
private
void PrepraringPizza() //Customer
dont need to know about how to cook pizza.
{
Console.WriteLine("Pizza is baking....");
}
private
void PackingPizza() //Customer
dont need to know the process of packing.
{
Console.WriteLine("Pizza is packing in a box....");
}
}
class Program
{
static void Main()
{
Pizza
pizza = new Pizza();
pizza.Flavour = "Fajita"; //Customer
wants different Flavours of Pizza.
pizza.Quantity = 1; //Customer
wants different Quantity of Pizza.
pizza.OrderPizza();
pizza.ReceivingPizza();
Console.ReadLine();
}
}
}