What is Encapsulation in OOPS

What is encapsulation in OOPS (Object Oriented Programming) is a very common question asked by new programmers. The answer to this question is very easy but first, let me give you an example from the real world. Have you ever wondered why do we use capsules in medicines? Those colorful medicine capsules is an example of encapsulation. The manufacturers of that medicine do not want the medicines inside the capsule to do some chemical reactions with the air and gases in the environment.

What-is-Encapsulation-in-OOPs

Some medicines are so complex in nature that if they come in contact with the air, their properties will be changed and they will not work as per our expectations. This is why they are tightly locked inside that capsule and delivered to the body.

What is Encapsulation in OOPS

Encapsulation is biding the Variables of a class with the methods and accessing it in a controlled manner.

As we all know that when we make a class that class will have some properties and some methods inside. There are two ways of accessing the variables.

  1. Keep the variables public and access the data with the class objects. (Not Recommended by Microsoft)
  2. Keep the data private and access the data with the help of methods. (Using Encapsulation)

Example of Keep the variables public and access the data with the class object

using System;


namespace Encapsulation
{
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            person.Name = "John Cena";
            person.Age = -1;
            person.Address = "New York";

            Console.WriteLine(person.Name);
            Console.WriteLine(person.Age);
            Console.WriteLine(person.Address);

            Console.ReadLine();
        }
    }
}

As you can see the above example, we have kept the properties as public and access the properties with class object. You can see we can get the value of the properties and set the values of properties easily.

What is the problem with this way of accessing the data you might ask, the problem is that the data is not being accessed in a controlled manner. Look at the value of age. We have set the value as -1 and there is no error. This is illogical to set the value as -1 hence there should be an error.

In order to access these properties in a controlled way, we will have to use Encapsulation.

Related Posts

How do we Achieve Encapsulation

We achieve Encapsulation by keeping the variables private and access them by methods. We need to create two types of method for a single variable.

  1. GetVariable()
  2. SetVariable()

The above methods are also called Getter and Setter for a variable. Let me show you the same example with Encapsulation.

using System;

namespace Encapsulation
{
    class Person
    {
        private string Name { get; set; }
        private int? Age { get; set; } = null;
        private string Address { get; set; }

        public string GetName()
        {
            return Name;
        }

        public void SetName(string Name)
        {
            this.Name = Name;
        }

        public int? GetAge()
        {
            return Age;
        }

        public void SetAge(int Age)
        {
            if (Age > 1)
            {
                this.Age = Age;
            }
            else
            {
                Console.WriteLine("Age can not be below 1");
            }
        }

        public string GetAddress()
        {
            return Address;
        }

        public void SetAddress(string Address)
        {
            this.Address = Address;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            person.SetName("John Cena");
            person.SetAge(5);
            person.SetAddress("New York");

            Console.WriteLine(person.GetName());
            Console.WriteLine(person.GetAge());
            Console.WriteLine(person.GetAddress());

            Console.ReadLine();
        }
    }
}

See the above example we are accessing the properties and setting their values in a controlled way. The properties are marked as private so they can not be accessed by an instance of a class.

Why do we use Encapsulation

  1. Checking the identity of a user before the user accesses a property.
  2. Checking the identity of a user before the user Sets a property.
  3. Setting a condition before the user accessing a property.
  4. Setting a condition before the user sets a property.

Suppose you are creating an employee management application, and you want only people from the HR department to change the Employee information. This condition can be set inside the Setter method. Just like we have set a condition in the above example where the value of age property can not be set before 1.

Suppose you want people from the Transport department to see your address and name, this condition can be set inside the getter method.

As we have talked about in our previous articles, the most important thing nowadays is the data. The companies like Google, Facebook, IBM are spending billions of dollars to keep the data secured.

I hope you have found the answer to What is encapsulation in OOPs. In case of any queries feel free to comment. I will be glad to assist you.

Till then stay safe stay healthy.