What Is An Encapsulation

What is an encapsulation?

Encapsulation is an Object Oriented programming base concept. Encapsulation means protect important data inside the class which we do not want to be exposed outside the class.

Encapsulation process means binding the data members (variable, properties) and member function (Methods) in a single unit. Class is one of the best an example of an encapsulation.

Why do we need an encapsulation?

An encapsulation complements abstraction. Encapsulation hides private or unwanted data from outside the class. An abstraction displays only important features of a class. (Unwanted means other class and assemblies).With encapsulation, we can make variables, properties and methods private, so it is not accessible to all but accessible through the proper channels only to protect it from the accidental corruption from the other classes.

Let’s take the example of the mobile; we are taking a photo with the camera, we just show picture in mobile display, do not think about how to work with the mobile camera, mobile RAM or circuits? We just click a button on display and take a photo. In this case, mobile display is useful to us and not a mobile inside feature functionality. Hence, the mobile inside feature is an encapsulation.


[Image Source from Google]

Advantages of an encapsulation:

An encapsulation field of a class basically can be a write only or a read only. A class can change the data type of its fields anytime but the users of this class do not need to change any code. Encapsulation can protect your data from the accidental corruption. It provides better control of class field.

Data encapsulation is implemented by using access modifiers. It defines the scope and visibility of a class member. Type of access modifiers are shown below.

  • Public- Access to all the code in the program.
  • Private- Access to only members of the same class.
  • Protected- Access to the members of the same class and its derived classes.
  • Internal- Access to the current assembly.
  • Protected Internal- Access to the current assembly and the types derived from containing class.

Code example

Go to Visual Studio - Go to Console Application.

First, we will learn about the property in a class, as shown in the example given below.


In the example given above, i.e. create a class Encapsulation and define a Name variable as the string. We are also using here GET and SET property to Name variable. Set debug break point and run the code (press F5).


See in the figure given above. Here, first call Main method and afterwards, call name property and main method.

The output of the figure is given below.


We are seeing an output is blank.

Now, we are passing the name value, as shown below.


Now, run the code again and see what happens. How to React code with debug break point?


Now, change calling cycle, as shown in the above figure and it gives the output, as shown below.


From the above, we can see the use of an encapsulation with the properties. The property has divided into two parts GET and SET.

  • GET returns the value of the property field.
  • SET sets the value of the property field with the contents of the values.

Sample code 

  1. using System;  
  2. namespace OPPs {  
  3.     class Encapsulation {  
  4.         private string Name = "Alex";  
  5.         public string EmployeeName { // Property  
  6.             get {  
  7.                 return Name;  
  8.             }  
  9.             set {  
  10.                 Name = value;  
  11.             }  
  12.         }  
  13.         static void Main(string[] args) {  
  14.             string Name2 = string.Empty;  
  15.             // use properties  
  16.             Encapsulation e = new Encapsulation();  
  17.             Name2 = e.EmployeeName;  
  18.             Console.WriteLine("Employee Name: " + Name2);  
  19.             Console.ReadLine();  
  20.         }  
  21.     }  
  22. }   

In this blog, I discussed why encapsulation is important in an Object Oriented programming language and also some information about the properties.

Have a nice day.

Ebook Download
View all

OOP/OOD

Read by 0 people
Download Now!
Learn
View all