Encapsulation In C#

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, Abstraction and Encapsulation.

Today we will discuss Encapsulation. Most of the interviewers ask about encapsulation and how to use it in the code.

Encapsulation means encapsulate some logic. This is a mechanism to hide the secure or unwanted data to user. So, this is basically a concept of hiding the code [show only essential data of object - It is abstraction]. Actually, Encapsulation is used hide the member function, variable, etc. to the outside world.

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

  1. publicclassEmployee   
  2. {  
  3.     privatestring _employeeName;  
  4.     publicstringEmployeeName   
  5.     {  
  6.         get   
  7.         {  
  8.             return _employeeName;  
  9.         }  
  10.         set   
  11.         {  
  12.             _employeeName = value;  
  13.         }  
  14.   
  15.     }  
  16. }  
  17. publicclassDepartmentmain {  
  18.     publicstaticvoid Main(string[] args)   
  19.     {  
  20.         Employeeemp = newEmployee();  
  21.         emp.EmployeeName = "Mukesh Kumar";  
  22.         Console.WriteLine("Employee Name is " + emp.EmployeeName);  
  23.     }  
  24. }  
In the preceding example, you can see that there is a member variable _employeeName that is private, you can set and get the value using this but when you are going to expose the value to outer world, you need to expose Property EmployeeName.

Why Encapsulation

If you think, there is some code that should not be exposed to outside code. There you can use Access Modifier to encapsulate the data.

Real Time Example

Let us take an example of TV, When we watch TV, we need only TV and Remote. To use Remote Key we can manage everything of a TV. There is no one interesting in knowing what is happening when we press the Remote button.

Using the Remote, we can increase the Volume, change the channel, and change the color of Screen, etc. But we never think how it is happening in the backend.

Actually in Remote System, on every key press there is some logic behind this. That is not useful for everyone. Remote encapsulate the logic and when we press the key, it performs the action. So, this is a real time example of Encapsulation. Not only Remote, you can see lot of devices around you which use Encapsulation like your phone, your car, etc.
  1. classTV  
  2. {  
  3.     //background method to increase the volume of TV on key press  
  4.     privatevoidVolumePlus()  
  5.         {  
  6.             Console.WriteLine("Volumen Plus");  
  7.         }  
  8.         //background method to decrease the volume of TV on key press  
  9.     privatevoidVolumeMinus()   
  10.         {  
  11.             Console.WriteLine("Volumen Minus");  
  12.         }  
  13.         //color change of Color Tube on key press  
  14.     privatevoidColorTube()   
  15.         {  
  16.         Console.WriteLine("Color Tube of a Television");  
  17.         }  
  18.     publicvoidTVKeys()   
  19.     {  
  20.         Console.WriteLine("Television Keys");  
  21.     }  
  22.     publicvoidTVRemote() {  
  23.         Console.WriteLine("Television Remote");  
  24.     }  
  25.     publicvoidTVScreen() {  
  26.         Console.WriteLine("Television Screen");  
  27.     }  
  28.   
  29. }  
Above example will clear your doubts, you can see in above example some methods are private. It is because they will perform the logic on key press in background. We don’t need to know about this. But some are public, that will be accessible to us.

In C#, we use Access Modifier to hide the data from the outer world. The following are the access modifiers.

Private

It is used to restrict the member function or variable to be called outside from the class. If we create any function or variable inside the class as private then it will not be accessible to outside the class.

Public

A member function or variable declared as public can be accessed from outside the class. It means, you can access public member from anywhere.

Internal

The member functions and variables which are declared as Internal only can be accessible inside the same namespace. You cannot access these members outside the namespace where these are declared.

Protected

The Protected members can only be accessible from its child class or in same class. It is best when you are using inheritance in your project.

Protected Internal

It can be accessible within the assembly where it is declared or in derived class of other assembly.

To know more about Access Modifier, Visit MSDN link.

Thanks for reading this article, hope you enjoyed it.