Difference between Abstraction and Encapsulation in OOPs

Abstraction

Abstraction allows us to represent complex real world in simplest manner. It represent the necessary feature without representing the back ground details. Abstraction is a process of hiding work style of an object and showing only those information which are required to understand the object. Abstraction means putting all the variables and methods in a class which are necessary.

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

In Encapsulation, generally to hide data making it private and expose public property to access those data from outer world. Encapsulation is a method for protecting data from unwanted access or alteration. Encapsulation is the mechanism by which Abstraction is implemented.

Hope you now have a basic idea about both of these properties. Let’s see a real world example of encapsulation and abstraction.

Hope you now have a basic idea about both of these properties. Let’s see a real world example of encapsulation and abstraction.

There will be four fields:

  1. Name
  2. Email
  3. Phone Number
  4. City

So these inputs have to validate first and then insert into data into database

Now First, create a class with all methods as below code snipes:

namespace OppsConcept
{
    class UserData
    {
            public bool AddUser(string name, string email, string phone, string City)
            {
                if (ValidateUserInfo(name, email, phone,City))
                {
                    if (InsertDb(name, email, phone,City))
                    {
                        return true;
                    }
                }
                return false;
            }

            private bool ValidateUserInfo(string name, string email, string phone, string City)
            {
                // write validation code here
                return true;
            }

            private bool InsertDb(string name, string email, string phone, string city)
            {
                // Write database insert code here
                return true;
            }
     }

As you see, there are three methods  are written in this UserData class.

  • AddUser:  To call from outside the class. That is why the access modifier is public.
  • ValidateUserInfo:  To validate the user’s details. Can’t access from outside the class. because It’s private.
  • InsertDb:  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.

To call the AddUser method, do as follows:

   class Program
    {
        static void Main(string[] args)
        {

            UserData u = new UserData();
            bool bl = u.AddUser("ABC", "example@abc.com", "1122334455", "Ahmedabad");
        }
    }

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.

Difference between Abstraction and Encapsulation in C#

Abstraction Encapsulation
Abstraction solves the problem in the design level. Encapsulation solves the problem in the implementation level.
Abstraction is used for hiding the unwanted data and giving only relevant data. Encapsulation is hiding the code and data into a single unit toprotect the data from outer world.
Abstraction is set focus on the object instead of how it does it. Encapsulation means hiding the internal details or mechanics of howan object does something.
Abstraction is outer layout in terms of design.

For Example: – Outer Look of a Phone, like it has a display screen,calling….

Encapsulation is inner layout in terms of implementation.

For Example: – Inner Implementation detail of a Phone, how DisplayScreen are connect with each other using circuits

Hope you have cleared your doubts about the Difference between Abstraction and Encapsulation in OOPs.

Also Learn More Tutorial :

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *