Introduction
There are four major properties of Object Oriented Programming (OOPs) as you know. These are,
- Abstraction
- Encapsulation
- Inheritance
- 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.
- Name
- Email
- PhoneNo
So, these inputs have to be validated first and then, inserted into the database.
First, create a class with all methods.
-
- public class Users
- {
- #region Public methods
-
-
- public bool AddUser(UsersEnity userEntity)
- {
- var isSuccess = false;
- if (ValidateUser(userEntity))
- {
- if (Save(userEntity) > 0)
- {
- isSuccess = true;
- }
- }
- return isSuccess;
- }
- #endregion
-
- #region Private methods
-
- private bool ValidateUser(UsersEnity userEntity)
- {
- var isValidate = false;
-
-
-
-
-
- return isValidate;
- }
-
-
- private int Save(UsersEnity userEntity)
- {
- var rowsAffected = 0;
-
-
-
- return rowsAffected;
- }
- #endregion
- }
-
- public class UsersEnity
- {
- public string Name {get;set;}
- public string Email {get;set;}
- public string PhoneNo {get;set;}
- }
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.
- public class Program
- {
- static void Main(string[] args)
- {
- User objUser = new User();
- bool f = objUser.AddUser(new UsersEnity()
- {
- Name = "XYZ",
- Email = "[email protected]",
- PhoneNo = "9876543210"
- });
- }
- }
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.