Click here to Skip to main content
15,881,678 members
Articles / Programming Languages / C#

Difference between Encapsulation and Abstraction in OOPs (C#.NET)

Rate me:
Please Sign up or sign in to vote.
4.62/5 (15 votes)
7 Oct 2015CPOL2 min read 157.2K   11   9
Difference between encapsulation and abstraction in OOPs

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 of these four properties or features, we will discuss the first two (Abstraction and Encapsulation) in this post.

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 calling. 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 kits are available. And also these kits are available as loose in market. But the shaving kit encapsulates every other kit into a small bag and provides a user to use it.

Hope you now have a basic idea about 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 it to other developers to use. So first, create a class and add a method to insert the data into database with validation.

There will be three fields:

  1. Name
  2. Email
  3. Phone number

So these inputs have to validate first and then insert into db.

First, create a class with all methods:

C#
class User
{
    public bool AddUser(string name, string email, string phone)
    {
        if (ValidateUser(name, email, phone))
        {
            if (AddtoDb(name, email, phone) > 0)
            {
                return true;
            }
        }
        return false;
    }

    private bool ValidateUser(string name, string email, string phone)
    {
        // do your validation
        return true;
    }

    private int AddtoDb(string name, string email, string phone)
    {
        // Write the Db code to insert the data
        return 1;
    }
}

As you can see, there are three methods that are written in this User 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.
  • AddtoDb: To insert data into database table and again it is private, can't access from outside the class.

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

To call the AddUser method, do as follows:

C#
class Program
{
    static void Main(string[] args)
    {
        User objUser = new User();
        bool f = objUser.AddUser("Arka", "ark@g.com", "1234567890");
    }
}

Now come back to the main discussion.

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

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

Hope you have cleared your doubts about the concepts of Encapsulation and Abstraction.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer PwC
India India
I am a Software developer having an experience of 5 years in application development. To get me you can mail me at arkadeepde@gmail.com or you can visit my blog at ASP With Arka

Comments and Discussions

 
QuestionDifference between Encapsulation and Abstraction Pin
Member 1474532514-Feb-20 22:02
Member 1474532514-Feb-20 22:02 
Questionabout abstaraction and encapsulation Pin
Member 111037732-Feb-20 21:20
Member 111037732-Feb-20 21:20 
QuestionWrong Example of Abstraction Pin
DoingWork8-Jan-20 19:29
DoingWork8-Jan-20 19:29 
QuestionDifference between Encapsulation and Abstraction Pin
Member 1096666329-Aug-19 18:39
Member 1096666329-Aug-19 18:39 
PraiseSimple and Clear Explanation Pin
Oyeyinka Onaolapo19-Jun-19 8:05
Oyeyinka Onaolapo19-Jun-19 8:05 
GeneralMy vote of 5 Pin
Joy Acharya,9018-Sep-16 18:50
Joy Acharya,9018-Sep-16 18:50 
GeneralRe: My vote of 5 Pin
Arkadeep De5-Nov-16 7:59
professionalArkadeep De5-Nov-16 7:59 
QuestionEdward V. Berard Abstraction, Encapsulation and Information Hiding Pin
Me At Here8-Oct-15 4:07
Me At Here8-Oct-15 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.