Encapsulation in Object Oriented Programming (OOPS) Concept


Encapsulation , Abstraction and Inheritance are 3 key concepts in Object Oriented Programming (OOPS) C#.Net . Encapsulation allows an object to separate its interface from its implementation. The data and the implementation code for the object are hidden behind its interface.


Encapsulation hides internal implementation details from users.

Example:
A Customer may issue a check and now know how it is processed. The internal processing is hidden from the customer. Similarly, the inessential attributes of a class are hidden from users by using encapsulation. The hidden attributes of a class are called protected attributes.
It ensures that an object supplies only the requested information to another object and hides inessential information.
Example: When a user selects a command from a menu in an application, the code used to perform the actions of that command is hidden from the user.
Encapsulation packages the data and method of an object and provides protection from external tampering by users. It implies that there is visibility to the functionalities offered by an object, and no visibility to its data. The best application of encapsulation is making the data fields private and using public accessor methods.
However, you cannot hide an entire object. To use an object, a part of it needs to be accessed by users. To provide this access, abstraction is used. Abstraction provides access to a specific part of data while encapsulation hides the data. Therefore, abstraction and encapsulation compliment each other.


public class Employee
    {
        private string EmpName;

        // Accessor.
        public string GetEmpName()
        {
            return EmpName;
        }
        // Mutator.
        public void SetEmpName(string a)
        {
            EmpName = a;

        }

    }


Like the above way we can protect the private data from the outside world. Here we use two separate methods to assign and get the required data.

  public static int Main(string[] args)
    {
        Employee d = new Employee();
        d.SetEmpName("kapil");
        Console.WriteLine("The Employee Name is :" + d.GetEmpName());
        return 0;
    }



In the above example we can't access the private data EmpName from an object instance. We manipulate the data only using those two methods.


Encapsulation defines the access levels for elements of that class. These access levels define the access rights to the data, allowing us to access the data by a method of that particular class itself, from an inheritance class, or even from any other class. There are three levels of access:
·         public: functions of all classes may access the data or methods of a class that is defined with the public access level. This is the lowest level of data protection
·         protected: data access is restricted to functions of inheritance classes, i.e. member functions of that class and all sub-classes
·         private: data access is restricted to methods of that particular class only. This is the highest level of data protection



Conclusion: 
Encapsulation hides internal implementation details from users.

0 Responses to “Encapsulation in Object Oriented Programming (OOPS) Concept”

Post a Comment

Labels

Topics