Abstraction vs. encapsulation in C#
There are lot of confusion between developers when it comes to these important principle of OOPs i.e. abstraction
and encapsulation
. I think the reason is they have some similar definitions. Look at there definition- Abstartion
is nothing but showing only what is necessary while Encapsulation is hide complexity.
So, they both have almost same definition but in different sentences that's why some developers get confused with these two. In this article I will try to explain the differences between these two.
Difference between Abstraction and Encapsulation
Lets we have to create any UI with two fields i.e. Employee Name
and Employee Code
with Submit button to add these
two fields into database using ADO.Net with C#. For this purpose we need a class EmployeeDetails having two fields i.e. EmployeeName
and EmployeeCode
with one Submit() function to insert employee information in to database. See the below code:
public class EmployeeDetails { public string EmployeeName { get; set; } public string EmployeeCode { get; set; } public void Submit() { //Submit employee information into DB } }
Now, this class having what we have to create on basis of UI. But wait a moment, where is validations (to check duplication of employees, to check null/blank values etc)? where is ADO.Net connection methods? Let's add two more methods (i.e. ValidateEmployee() and CreateDBConnection) to achieve above two functionalities-
public class EmployeeDetails { public string EmployeeName { get; set; } public string EmployeeCode { get; set; } public void Submit() { //Submit employee information into DB } public bool ValidateEmployee() { return true; } public bool CreateDBConnection() { // TO DO: ADO.Net logic return true; } }
So, have we done? Not yet. Suppose, if we have to add any employee by using above class then it can be done something like-
class Program { public static void Main(string[] args) { EmployeeDeails emp=new EmployeeDetails(); emp.EmployeeName="Kapil"; emp.EmployeeCode="100009048"; emp.Validate();// to validate emp.CreateDBConnection(); emp.Submit(); } }
But if somebody is going to use this EmployeeDeails
class then he has to understand these all things i.e. call validate()
before
Submit()
. So that is very complicated procedure. So it'd be great if we can provide some more simplify interface. So what is making more complexity
in above class. The answer is that user should not call Validate() or CreateDBConnection()function. We are actually making user life complicated.
So, our EmployeeDetails
class will be looks like (hide unnecessary information- make ValidateEmployee() and CreateDBConnection() as private) -
public class EmployeeDetails { public string EmployeeName { get; set; } public string EmployeeCode { get; set; } public void Submit() { ValidateEmployee(); CreateDBConnection(); //Submit employee information into DB } private bool ValidateEmployee() { return true; } private bool CreateDBConnection() { // TO DO: ADO.Net logic return true; } }
Now, end user need to call only Submit() function. So, what we did? Well, we exposed only minimal things (encapsulation). Now let's start that what steps we followed to create this program.
So, where was encapsualtion and where was abstraction? In Step 1 there was abstraction and in Step 3 where we hiding complexity/unnecessary information is an Encapsulation()
So, abstraction is basically a thought process or modeling terms and encapsualtion is bring down complexity.