.Net

Abstraction and Encapsulation in C#

In this topic we are going to see the difference between Abstraction and Encapsulation in C#. So lets start.

Encapsulation is all about wrapping data and Abstraction is about hiding the implementation details.

Encapsulation 

It is the concept of OOP in which we hide the internal details or state of a class from the outside word. The fields or variables of the class define the state of a class. If we make these  fields public then anyone outside the class can access it and should be able to change the state of class.

  • Wrapping up a data member and a method together into a single unit (in other words class) is called Encapsulation.
  • Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.
  • Encapsulation means hiding the internal details of an object, in other words how an object works.
  • Encapsulation prevents clients from seeing its inside view, where the behavior of the abstraction is implemented.
  • It is a technique used to protect the information in an object from another object.
  • Hide the data for security such as making the variables private, and expose the property to access the private data that will be public.
  • Encapsulation is like you can play music in either of mobile. It means this is the property of encapsulating members and functions.

Abstraction 

In object Oriented programming if objects are the heart of modelling, then why bother with classes? The notion of abstraction is at the heart of matter. By grouping objects into classes we abstract a problem. Abstraction gives modelling its power and ability to generalize from a few specific cases to a host of similar cases.

  • It is “To represent the essential feature without representing the background details.”
  • Lets you focus on what the object does instead of how it does it.
  • It provides you a generalized view of your classes or objects by providing relevant information.
  • It is the process of hiding the working style of an object, and showing the information of an object in an understandable manner.

Now we will see the Real World Examples of both of them.

Example

Let’s assume you have to create a method to insert users data and pass it to other developers to use. So first, create a class and add a method to insert the data into database with validation.

There will be three fields:

  1. Name
  2. Email
  3. Phone number

So these inputs have to validate first and then insert into db.

First, create a class with all methods:

class User
{
    public bool AddUser(string name, string email, string phone)
    {
        if (ValidateUser(name, email, phone))
        {
            if (AddtoDb(name, email, phone) > 0)
            {
                return true;
            }
        }
        return false;
    }

    private bool ValidateUser(string name, string email, string phone)
    {
        // do your validation
        return true;
    }

    private int AddtoDb(string name, string email, string phone)
    {
        // Write the Db code to insert the data
        return 1;
    }
}

As you can see, there are three methods that are written in this User class.

AddUser: To call from outside the class. That is why the access modifier is public.
validateUser: To validate the user’s details. Can’t access from outside the class. It’s private.
AddtoDb: To insert data into database table and again it is private, can’t access from outside the class.
Now another user will just call AddUser method with parameters. And that user has no idea what is actually happening inside the method. I didn’t write the code to validate and insert into db, as you can get it from others examples. We will discuss about it later.

To call the AddUser method, do as follows:

class Program
{
    static void Main(string[] args)
    {
        User objUser = new User();
        bool f = objUser.AddUser("Suman", "sumank@g.com", "5475743466");
    }
}

Now come back to the main discussion.

Here, we are hiding the procedure of adding data into database from other users, this is Abstraction. And putting all the three methods into one User class and providing other users to use it, that is called Encapsulation.

So procedure hiding is Abstraction and putting every necessary thing into one is Encapsulation.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

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

Back to top button