Difference between Encapsulation and Abstraction

Encapsulation

  1. reduce complexity
  2. hide the information which you don't want to share with outside world
In simple word encapsulation is information hiding

            

Abstraction

  1. Show only the essential features
  2. hide internal process



In simple word abstraction is implementation hiding

Example (Console Program)

    class CustomerClass
    {
        public void AddCustomer(string name)
        {
            validate("uttam");
            savedata("uttam");
        }

        private bool validate(string name)
        {
            //Validate name
            return true;
        }

        private bool savedata(string name)
        {
            //Save to database
            return true;
        }

    }

    class Program
    {
        static void Main(string[] args)
        {            
            CustomerClass p = new CustomerClass();
            p.AddCustomer("uttam");            
        }        
    }

Where is Encapsulation and Abstraction in the above example?

Encapsulation:  When we make function validate and savedata access modifier to private then we are doing encapsulation.

Abstraction: We are allowing function AddCustomer to be accessed from outside world and hiding the internal process like validate and savedata is abstraction.
            

Comments

Popular posts from this blog

SQL Interview Questions and Answers

Generic Interface and Factory Pattern in C#

How to get device info IMEI programmatically in xamarin android