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.
- publicclassEmployee
- {
- privatestring _employeeName;
- publicstringEmployeeName
- {
- get
- {
- return _employeeName;
- }
- set
- {
- _employeeName = value;
- }
-
- }
- }
- publicclassDepartmentmain {
- publicstaticvoid Main(string[] args)
- {
- Employeeemp = newEmployee();
- emp.EmployeeName = "Mukesh Kumar";
- Console.WriteLine("Employee Name is " + emp.EmployeeName);
- }
- }
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.
- classTV
- {
-
- privatevoidVolumePlus()
- {
- Console.WriteLine("Volumen Plus");
- }
-
- privatevoidVolumeMinus()
- {
- Console.WriteLine("Volumen Minus");
- }
-
- privatevoidColorTube()
- {
- Console.WriteLine("Color Tube of a Television");
- }
- publicvoidTVKeys()
- {
- Console.WriteLine("Television Keys");
- }
- publicvoidTVRemote() {
- Console.WriteLine("Television Remote");
- }
- publicvoidTVScreen() {
- Console.WriteLine("Television Screen");
- }
-
- }
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.