Virtual Classes Logo          Virtual Classes
    Read Anytime,Anywhere


Encapsulation is the packing/Wrapping of information so that the imformation cann't be accessed out side world.

In programming we can define encapsulation as,
"Encapsulation is a process of binding data members (variables, properties) and member functions (methods) into a single unit".

We can achieve encapsulation through Classes or Structures. Through encapsulation a class can hide the internal details of how an object does something. A class or structure can specify how accessible each of its members (variables, properties, and methods) to code outside of the class or structure. Encapsulation simplifies the interaction between objects.
This can also be understood using a motor bike example. A bike has actions such as 'switch on light', 'horn' etc. and it's attributes such as specific color,brand etc. Here the actions and attributes are bundled together under a single unit, bike.
A employee object has name, address, company, and bank account properties. If employee's comany need any information of employee i.e. bank account, it can request to employee object to provide the same and without knowing other details of an employee.
With the help of encapsulation, a class can change the internal implementation without hurting the overall functionality of the system.

Benifit of encapsulation-
1-Encapsulation reduces system complexity, and thus increase robustness
2-To hide and prevent code (data) from the outside world (here the world means other classes and assemblies).
3-To prevent code (data) from accidental corruption due to programming errors so that we can deliver expected output. Due to programming mistakes, code may not behave properly and it has an effect on data and then it will affect the functionality of the system. With encapsulation we can make variables, properties, and methods private so it is not accessible to all but accessible through proper channels only to protect it from accidental corruption from other classes.
4-To have a class better control over its fields (validating values etc…).

Ways to achieve encapsulation with code example
If a function or a method inside this class is private,only objects of this class can access that method/function.access specifiers determine the accessability of the methods.
public class BankAccount
private int accountNo;

#region Encapsution by using methods
// get methods
public string GetAccountNo()
return accountNo ;
// Set method
public void SetAccountNo(int accNo)
accountNo= accNo;

#region Encapsution by using properties

// property which has get and set
public string AccoutNo

return accountNo;
accoutNo= value;

static void Main()
int accNo=12345;
BankAccount bankAcc= new BankAccount();
// Encapsulation using methods
// Encapsulation using properties


In the above example we use the get and set methods (GetAccountNo and SetAccountNo) to get account no and set account no.
We use the private variable accountNo and the variable is private so it cann't e access directly out side of the class, to use this variable, we use the get and set methods or AccountNo property.