C#

Encapsulation in C Sharp


As we know the most popular languages like C# and Java is based on OOPs that is based on object. They use all the features of Object Oriented Programming. OOPs have many features such as Inheritance , Polymorphism  and Abstraction .


In this article we will discuss Encapsulation

So, Encapsulation is a way of encapsulating the data into a single unit called class. Through encapsulation, we can protect our data.

Encapsulation means to encapsulate or put everything into one thing and provide others to use it
This binds the member function and data member into a single class. This also allows for abstraction. Within OOP, encapsulation can be achieved through creating classes. Those classes then display public methods and properties. The name encapsulation comes from the fact that this class encapsulates the set of methods, properties, and attributes of its functionalities to other classes.

Let's see a real time example of encapsulation
Let's assume you have to create a method to insert employee details and pass it to other developers to use it. So first of all we create a class and add a method to insert the data into database with validation.

There will be four fields:
1.         EmployeeName
2.         EmployeeEmail
3.         EmployeePhoneNumber
4.         EmployeeAddress

So these inputs have to validate first and then insert into database.
First, create a class with all methods:
Encapsulation in C#

class Employee
    {
        public string EmployeeName { get; set; }
        public string EmployeeEmail { get; set; }
        public string EmployeePhoneNumber { get; set; }
        public string EmployeeAddress { get; set; }

     
       
    }
    class AddEmployee
    {
        public bool SaveEmployee(Employee obj)
        {
            if (ValidateEmployeeDetail(obj))//if validate then we save in data base otherwise return false
            {
                if (AddtoDataBase(obj) > 0)//if inserted successfully then return true else return false
                {
                    return true;
                }
            }
            return false;
        }

        private bool ValidateEmployeeDetail(Employee obj)
        {
           //here you can implement your validation logic
            return true;
        }

        private int AddtoDataBase(Employee obj)
        {
            // Write the code for inserting data into database
            return 1;
        }
    }
As you can see, there are four methods that are written in this AddEmployee class.
          SaveEmployee: To call from outside the class. That is why the access modifier is public.
          ValidateEmployeeDetail: To validate the user's details. Can't access from outside the class. It's private.
          AddtoDataBase: To insert data into database table and again it is private, can't access from outside the class.
Now another user will just call SaveEmployee 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 database, here I am just telling you about encapsulation. We will discuss about it later.

Here i showing, How we call the saveEmployee method, please see below
Encapsulation in C#

class Program {
       
        static void Main(string[] args)
        {
            Employee empObj = new Employee {
                EmployeeAddress = "xyz",
                EmployeeEmail = "abc@gmail.com",
                EmployeeName="abc",
                EmployeePhoneNumber="8764564534"
            };
            AddEmployee obj = new AddEmployee();
            bool result=obj.SaveEmployee(empObj);
            Console.ReadKey();
        }
    }
So, here we are putting all the four methods into one AddEmployee class and providing other users to use it, that is called Encapsulation.
So putting every necessary thing into one is Encapsulation.

Hope you have cleared about the concepts of Encapsulation



1 comment: