Difference Between Encapsulation And Abstraction In Object Oriented Programming

Introduction

There are four major properties of Object Oriented Programming (OOPs) as you know. These are,

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

Among these four properties or features, we will discuss the first two (Abstraction and Encapsulation) in this post. It is one of the common interview questions for all level of C# developers irrespective of companies.

Often, we use a bookish example to learn the differences; however, that we forget easily. In this article, we will use a real-life example so that we can remember the difference forever.

Description

So, what actually are Abstraction and Encapsulation? Let us find out with an example.

Abstraction

Abstraction is a process to abstract or hide the functionality and provide users or other programmers to use it only. Like for the method Console.WriteLine(), no one knows what actually is happening behind the function call. We are just using it by calling and passing the arguments. This is the thing called Abstraction.

Encapsulation

Encapsulation means to encapsulate or put everything into one thing and provide others to use it. Like in a shaving kit, all the necessary things are available. And also, these products are available as loose in the market. But the shaving kit encapsulates every other item into a small bag and is provided to a user as a whole.

Difference

Hope you now have a basic idea of both of these properties. Let's see a real-world example of encapsulation and abstraction.

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

There are three fields.

  1. Name
  2. Email
  3. PhoneNo

So, these inputs have to be validated first and then, inserted into the database.

First, create a class with all methods.

  1. // Users class to add new users  
  2. public class Users  
  3. {  
  4.     #region Public methods  
  5.     // Add new user   
  6.     // Input takes Name & phone number  
  7.     public bool AddUser(UsersEnity userEntity)  
  8.     {   
  9.       var isSuccess = false;  
  10.         if (ValidateUser(userEntity))  
  11.         {  
  12.             if (Save(userEntity) > 0)  
  13.             {  
  14.                 isSuccess = true;  
  15.             }  
  16.         }  
  17.       return isSuccess;  
  18.     }  
  19.     #endregion  
  20.  
  21.     #region Private methods  
  22.     // Validate whether user data is perfect or not  
  23.     private bool ValidateUser(UsersEnity userEntity)  
  24.     {  
  25.       var isValidate = false;  
  26.           
  27.         // do your validation for userEntity.Name  
  28.       // do your validation for userEntity.Email  
  29.       // do your validation for userEntity.PhoneNo  
  30.           
  31.         return isValidate;  
  32.     }  
  33.       
  34.     // add new users to DB  
  35.     private int Save(UsersEnity userEntity)  
  36.     {  
  37.       var rowsAffected = 0;  
  38.           
  39.         // write the DB code to insert the data (Linq or ADO.NET)  
  40.           
  41.         return rowsAffected;  
  42.     }  
  43.     #endregion  
  44. }  
  45.   
  46. public class UsersEnity  
  47. {  
  48.     public string Name {get;set;}  
  49.     public string Email {get;set;}  
  50.     public string PhoneNo {get;set;}  
  51. }  

As you can see, there are three methods that are written in this Users 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.

  • Save
    To insert data into a database table and again it is private, can't access from outside the class.

Now, another user will just call the 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 other examples. We will discuss it later.

To call the AddUser method, do as below.

  1. public class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         User objUser = new User();  
  6.         bool f = objUser.AddUser(new UsersEnity()  
  7.            {  
  8.              Name = "XYZ",  
  9.              Email = "[email protected]",  
  10.              PhoneNo = "9876543210"  
  11.         });   
  12.     }  
  13. }  

Now, come back to the main discussion.

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

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

I hope I have cleared your doubts about the concepts of Encapsulation and Abstraction.


Similar Articles