Lesson 10

C# Encapsulation and Abstraction with Programming Example


Objective

What is Abstraction in C#?
What is Encapsulation?
What is Access Modifiers?

There are four major pillars that make c# language to object oriented - Inheritance, polymorphism, Abstraction and Encapsulation. We will discuss about inheritance and polymorphism in next chapter. Here we will clear concept of Abstraction and Encapsulation with suitable programming examples and real world examples.

What is Encapsulation in c#?

It is not hard to understand concept of Encapsulation. In programming language Encapsulation is a process of hiding complex functionality into a single class. So the user will never know how this class is working but they will know how to use it. For example there is a method in c# Console.WriteLine(). How many among of you know the inner functionality of this method. But all you know how to use this method to print message on console. All the complex functionality is encapsulated into a single method Console.WriteLine().

Encapsulation

Let's understand it with real world example. In order to use camera to shoot photo you need not to know how light passes through prism. So the internal mechanism that is not beneficial for you; is encapsulated under camera body. You are only required shoot button and menu operation to operate camera. So the object is hidden from you is encapsulation and the object that is visible to you is abstraction.

What is Abstraction in c#?

Encapsulation hides complex structure and gives you easy option to use it. This option is known as abstraction. For example, you know how to use Console.WriteLine() with parameter or without parameter. For real world example of bike you know that changing gear to top will help in accelerate bike. However you don’t need to know how it works. So the functionality of encapsulation is known as abstraction.

Abstraction

Take one more real world example to clear concept of abstraction. The menu button, the shoot button, the zoom button, the on off button in camera are the perfect example of abstraction. You can operate your camera easily by using these visible buttons. When you click shoot button to take photos it looks very easy but all the complex tasks is done inside camera.

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace example
{
    class Camera
    {
        private void PrismStart()
        {
            Console.WriteLine("Prism Start");
        }

        private void LightEmulator()
        {
            Console.WriteLine("Light Recorded");
        }

        public void ShootButton()
        {
            Console.WriteLine("Shoot Button Pressed");
        }

        public void OnOffButton()
        {
            Console.WriteLine("Camera is shutting down");
        }

        public void menubutton()
        {
            Console.WriteLine("Menu button pressed");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Camera c = new Camera();
            //Do not uncomment following lines it will raise error.
            //the following method is not accessible because of its protection level.

            //c.PrismStart(); //inaccessible. It is encapsulated.
            //c.LightEmulator(); //Inaccessible. It is encapsulated.
            c.ShootButton();
            c.OnOffButton();
            c.menubutton();
            Console.ReadKey();
        }
    }
}

Output

Shoot Button Pressed
Camera is shutting down
Menu button pressed
__

Access Modifiers

C# Encapsulate its complex code into a single unit using access modifiers. The Abstraction and Encapsulation process is based on Access Modifiers. There are 5 types of access modifiers are available in c# programming.

  1. Private Access Modifiers
  2. Protected Access Modifiers
  3. Internal Access Modifiers
  4. Protected Internal Access Modifiers
  5. Public Access Modifiers

Summary

We will learn all these modifiers in next few chapters with programming example and will see how we can utilize these modifiers to make program encapsulate and abstract. Next chapter is describing Private Access Modifiers.

BackNext