Learn C#.NET, ASP.NET MVC 5,ASP.NET Core that you can increase your knowledge and coding to develop the real-time project.


Friday, April 13, 2018

Abstraction And Encapsulation In C#

In this tutorial, we will learn data Abstraction and Encapsulation in C# and I will provide real-time use of Data Abstraction and Encapsulation with the example in a simple way. I hope this will very helpful for you to develop the real-time project for fresher as well as experienced people.
Abstraction And Encapsulation In C#


What is the need for Data Abstraction?
If you want to hide your business logic from the outside world. To achieve this implementation we can use Data Abstraction.

What is Data Abstraction?:
It is used to provide the relevant information and hide the irrelevant information from the user it is called as data abstraction.
It will only expose the relevant information to the outside world. It will provide the necessary information to the user and hide unnecessary information from the user.

Example:
  1. Console.WriteLine();  

This is the example of data abstraction. The "Console.WriteLine()" method is provided by Microsoft and they have written a lot of code behind the "WriteLine()" method but, they don't show all the code to the user. They only provide what the user want means they provide only necessary information to the user. and hide unnecessary information from the user. The user doesn't need what code written behind the "WriteLine" method. so that, they expose the "WriteLine()" method to the outside world. The user can access easily. This is the concept of data abstraction.

Example 2:
real time example of abstraction in c#

TV or Car remote is the also example fo Abstraction.TV or Car remote is assembled from the collection of circuits but they don't show to the user all circuits behind the remote, They only provide remote to the user to use it. When the user presses the key on remote the channel gets changed. They provide only necessary information to the user. This concept is called Data Abstraction.

What is the need for Data Encapsulation?

If you want to hide the complexity and provide the security to data. To achieve this implementation the oops concept came i.e. Data Encapsulation.

What is Data Encapsulation?
It is used to bind or group the related data together, and hide the complexity, and provide the security for the data. It is called Data Encapsulation.

Non-Technical Example:
Abstraction And Encapsulation In C#
A capsule is the best example of encapsulation. A capsule is inside the wrapper and wrapper provide the security to the capsule and wrapper is work as a container like class.

Example:
  1. public class AbstractionAndEncapsulation  
  2.   
  3. {  
  4.   
  5.  // Class member declaration and definition here  
  6.   

The class is the best example of Encapsulation. It will encapsulate or put everything into one thing and provide others to use it. And also It will hide the complexity and provide the security for data.
We can say in a non-technical term Like a TV or Car remote. It assembled from the collection of circuits but they hide all circuits from the user. They encapsulate all circuits in one thing called remote and provide to the user to use it. And also remote can provide security to all circuits used inside the remote. This concept called Encapsulation.
I hope you now have a basic idea about both of these oops concepts. Let's see a real-world example of encapsulation and abstraction. 
Let's assume you have to create "AddUserDetail()" method to insert users data and pass it to other developers to use. So first, create a class named as "AbstractionAndEncapsulation" and "SaveUserDetailToDB()" a method to insert the data into the database. Create one more method for user validation named as "ValidateUser()"

Real-Time Use Of Abstraction and Encapsulation:

  1. public class AbstractionAndEncapsulation  
  2.    {  
  3.        public bool AddUserDetail(string name, string email)  
  4.        {  
  5.            if (ValidateUser(name, email))  
  6.            {  
  7.                if (SaveUserDetailToDB(name, email) > 0)  
  8.                {  
  9.                    return true;  
  10.                }  
  11.            }  
  12.            return false;  
  13.        }  
  14.        private bool ValidateUser(string name, string email)  
  15.        {  
  16.            // This method is used to validate the user detail and return true or false based on result.  
  17.            return true;  
  18.        }  
  19.        private int SaveUserDetailToDB(string name, string email)  
  20.        {  
  21.            // Write the code to save the data into Database  
  22.            return 1;  
  23.        }         
  24.    }  

As you can see, there are three methods that are written in above class.
  • AddUserDetail: This method can call from outside the class. It is a public method.
  • ValidateUser: To validate the user detail. This method can not call from outside the class. It is a private method.
  • SaveUserDetailToDB: To insert user data into database table. This method can not call from outside the class. It is a private method.
Now another user can call just "AdduserDetail()" method which is public. and another method cannot be accessed outside the world or class.
It means that the class AbstractionAndEncapsulation which we have created in the above example that achieves two concepts like Abstraction and Encapsulation. It will provide relevant information to the user. and It also groups the related data, and hide complexity, and provide security for the data. To call the "AddUserDetail()" method, do as follows:

  1. class Program  
  2.   {  
  3.       static void Main(string[] args)  
  4.       {  
  5.           AbstractionAndEncapsulation objUser = new AbstractionAndEncapsulation();  
  6.           bool isResult = objUser.AddUserDetail("Shri""shri@gmail.com");  
  7.           Console.WriteLine();  
  8.       }  
  9.   }  

Now come back to the main discussion.
Here, we are hiding the procedure of adding data into a database from other users, this is Abstraction. And putting all the three methods into one class and providing other users to use it, that is called Encapsulation.
  • We have two methods private so that, It does not access to other classes and also putting all the three methods into one class called as "AbstractionAndEncapsulation", So we can say we have achieved Encapsulation.
  • We have provided only "AddUserDetail()" method to the user, User doesn't know what we are doing with this method so we can say, We have achieved Abstraction here.
I hope you have cleared your doubts about the concepts of Encapsulation and Abstraction.

Following are the previews article on OOPs



References:

More Detail with real-time example visit following link:


Share:

8 comments:

Upcoming Articles/Videos

Design Pattern
SOLID Design Principles
Copyright © Programming With Shri | Powered by Shrimant Telgave Home | Disclaimer | Privacy Policy | Terms and Conditions Design by Shrimant Telgave