Theme-Logo
  •   .Net
    •   C Sharp(C#)
    •   Web API
    •   Micro Services
    •   ASP.Net
    • ASP.Net MVC
    • .Net Core
  •   Database
    • SQL Server
    • Oracle
    • PostgreSQL
  •   jQuery
    • jQuery Tutorials
    • jQuery Plugins
    • jQuery UI
    • More on jquery
  •   Tutorials
    • Microservices Tutorials
    • DotNet Core Tutorials
    • PostgreSql Tutorials

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.

  • Step: 1: Initially once we got requirement e.g. WHAT
  • Step: 2: We have created required program e.g. HOW
  • Step: 3: We simplify the whole process by exposing minimal things by making some method (ValidateEmployee() and CreateDBConnection() ) private.
  • 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.

    Trending Post
    Benefits of using ASP.NET Core over ASP.NET.
    Role of Startup class in .Net Core.
    Interview Questions and Answers
    What is Request delegate?
    What is Host in ASP.NET Core?
    Describe the Generic Host and Web Host?
    Describe the Servers in ASP.NET Core?
    How to read values from Appsettings.json file?
    How to handle errors in ASP.NET Core?
    About us

    DotNet Palace is a community platform created and maintained by The articles are mainly focused on Microsoft stack technologies like C#, ASP.Net, MVC, .Net Core, SQL Server and Oracle, PostgreSQL, SQLite etc. To improve the site's content you can send your valuable suggestions at info.dotnetpalace@gmail.com

    Quick links
  • SOLID Principles
  • Questions
  • OOPS Principles
  • DotNet Palace Tutorials
  • Privacy Policy
  • Terms and Condition