Abstraction

Introduction

In Simple terms, Abstraction is the process of hiding the irrelevant information in a higher degree and only showing essential thing to the user. It focusses on what the thing does instead of how it does.

For example, a mobile phone to make the call we don’t need to know how our call gets connected to another phone, we just need to dial number and press call button in order to make a call.

A similar way to turn on the lights we just need to press the button, no need to worry about how wires are connected internally, or how the light gets turn on button clicks.

In Computer, abstractions are essential and everywhere, because we don’t want to think about all the details like how electrons are moving in transistors, or how bits get converted.

These all examples represent abstractions. we can also relate why abstraction is essential from the above example.

Abstraction is “the process of identifying common patterns that have systematic variations; an abstraction represents the common pattern and provides a means for specifying which variation to use”

Richard Gabriel.

In object-oriented programming (OOPs), abstraction help in designing the applications. To achieve abstraction object-oriented programing used abstract class, interface. So, the client who uses the class or interface, should not care about how or why it was, the client should only aware of what can be done using this class or interface.

In designing software, abstraction helps to reduce the complexity of the applications and implementation process of software.

A real-time example of abstraction

Let’s understand the abstraction using a real-time example of Pizza, suppose we have Pizza machine which prepares pizza based on pizza type selection, the machine also provides facility to choose different topping to add on Pizza.

Depending on our choice of pizza, machine decide which items required to create the base of the pizza, add cheese, Sausage, etc. after creating the base of Dough Machine add topping on pizza and then bake it

Implementing the abstraction

Using the concept of abstraction, we can hide all decisions and processing steps required for a Pizza class. To create a simple application, it takes pizza type and toping details, and based on this information it creates a pizza.

using System;
using System.Collections.Generic;


namespace PizzaMachine
{
    public class Pizza
    {
        public void CreatePizza(PizzaType pizzaType, List<PizzaTopping> pizzaToppings)
        {
            Console.WriteLine("Pizza Creation Started...");
            GetBaseMaterial();
            PrepareDough();

            IPizza pizza = null;
            switch (pizzaType)
            {
                case PizzaType.Pepperoni:
                    pizza = new PepperoniPizza(pizzaToppings);
                    break;
                case PizzaType.Margherita:
                    pizza = new MargheritaPizza(pizzaToppings);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("Invalid Pizza Type");
            }
            pizza.CreateBase();
            AddTopping(pizzaToppings);
            Bake();
            Console.WriteLine("Pizza Ready");
        }

        public void GetBaseMaterial()
        {
            Console.WriteLine("Get Base Materials for Pizza");
        }

        public void PrepareDough()
        {
            Console.WriteLine("Prepare Dough for Pizza");
        }

        public void AddTopping(List<PizzaTopping> pizzaTopping)
        {
            Console.WriteLine($"Adding topping: {string.Join(",", pizzaTopping)}");
        }

        public void Bake()
        {
            Console.WriteLine("Bake Pizza");
        }

    }
}

PizzaType is a simple enum providing a set of predefined values for the different kinds of pizzas.

namespace PizzaMachine
{
    public enum PizzaType
    {
        Pepperoni,
        Margherita
    }
}

Similar way PizzaTopping is simple enum providing a set of predefined values for the different kinds of toppings.

namespace PizzaMachine
{
    public enum PizzaTopping
    {
        Pepperoni,
        Mushrooms,
        Onions,
        Sausage,
        Bacon,
        ExtraCheese,
        BlackOlives,
        GreenPeppers,
        PineApple,
        Ham
    }
}

So, to create a pizza we just need to provide pizza type and topping details to pizza class, it will create a pizza. we can see in this example that the abstraction provided by the pizza class hides all the pizza creation and process details. That makes it easy to use and allows each developer to focus on a specific class. if some clients implement pizza class then, the client doesn’t need to know anything about its internal processes.

abstraction makes the implementation of a complex application much easier. Also, each class has its own abstraction. I.e. MargheritaPizza abstract the complexity of creation base process

using System;
using System.Collections.Generic;

namespace PizzaMachine
{
    class MargheritaPizza : IPizza
    {
        private readonly List<PizzaTopping> _extraPizzaTopping;

        public MargheritaPizza(List<PizzaTopping> extraPizzaTopping)
        {
            _extraPizzaTopping = extraPizzaTopping;
        }


        public void CreateBase()
        {
            PreparePizzaBase();
        }

        public void PreparePizzaBase()
        {
            Console.WriteLine("Create Margherita Pizza");
            Console.WriteLine("Add tomato sauce");
            Console.WriteLine("mix of grated mozzarella");
        }
    }
}

Also PepperoniPizza class abstract the complexity of creation a Pepperoni Pizza base process.

using System;
using System.Collections.Generic;

namespace PizzaMachine
{
    class PepperoniPizza : IPizza
    {
        private readonly List<PizzaTopping> _extraPizzaTopping;

        public PepperoniPizza(List<PizzaTopping> extraPizzaTopping)
        {
            _extraPizzaTopping = extraPizzaTopping;
        }

        public void CreateBase()
        {
            PreparePizzaBase();
        }

        public void PreparePizzaBase()
        {
            Console.WriteLine("Create Pepperoni Pizza");
            Console.WriteLine("Add mixture of pork and beef seasoned with paprika");
        }
    }
}

Interface

namespace PizzaMachine
{
    interface IPizza
    {
        void CreateBase();
        void PreparePizzaBase();
    }
}

Main method

using System;
using System.Collections.Generic;

namespace PizzaMachine
{
    class Program
    {
        static void Main()
        {
            Pizza pizzaInstance = new Pizza();
            pizzaInstance.CreatePizza(PizzaType.Margherita,new List<PizzaTopping>() {PizzaTopping.Pepperoni});
            Console.ReadLine();
        }
    }
}

Code map of above program

abstraction in oops example, abstraction in oops meaning, abstraction in programming, abstraction in oops definition, abstraction in detail in C#, what is abstraction in oops, abstraction in oops with example

Read more about encapsulation in oops

Read more about OOPS Concept

Helpful Resource


Conclusion

Abstraction is the process of abstract internal implementation details which makes client life easier. As a client, we just need to know which method we need to call, rest all detail we should not be worried about. This makes application implementation much easier and more maintainable.

Please add comments below for any further information.


Leave a Reply

Your email address will not be published. Required fields are marked *