What is Encapsulation ? • Encapsulation is one of the fundamental principles of object-oriented programming.
• Encapsulation is a process of hiding all the internal details of an object from the outside world
• Encapsulation is the ability to hide its data and methods from outside the world and only expose data and methods that are required
• Encapsulation is a protective barrier that prevents the code and data being randomly accessed by other code or by outside the class
• Encapsulation gives us maintainability, flexibility and extensibility to our code.
• Encapsulation makes implementation inaccessible to other parts of the program and protect from whatever actions might be taken outside the function or class.
• Encapsulation provides a way to protect data from accidental corruption
• Encapsulation hides information within an object
• Encapsulation is the technique or process of making the fields in a class private and providing access to the fields using public methods
• Encapsulation gives you the ability to validate the values before the object user change or obtain the value
• Encapsulation allows us to create a "black box" and protects an objects internal state from corruption by its clients.
• There are two ways to create a validation process.
1. Using Accessors and Mutators 2. Using properties
Benefits of Encapsulation • In Encapsulation fields of a class can be read-only or can be write-only
• A class can have control over in its fields
• A class can change data type of its fields anytime but users of this class do not need to change any code
In this example _employeeid and _salary is private fields and providing access to the fields using public methods (SetEmployeeID,GetEmployeeID,SetSalary,GetSalary) In this example _employeeid and _salary is private fields and providing access to the fields using public methods (EmployeeID,Salary). Code Snippet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Encapsulation
{
class Employee
{
private int _employeeId;
private double _salary;
private int _leaveBalance;
private string _designation;
private string _employeename;
//****Mutators*****
public void SetEmployeeId(int emplid)
{
_employeeId = emplid;
}
public void SetSalary(double sal)
{
_salary = sal;
}
public void SetLeaveBalance(int leavedetails)
{
_leaveBalance = leavedetails;
}
public void SetDesignation(string designation)
{
_designation = designation;
}
public void SetName(string name)
{
_employeename = name;
}
//***Accessors******
public int GetEmployeeId()
{
return _employeeId;
}
public double GetSalary()
{
return _salary;
}
public int GetLeaveBalance()
{
return _leaveBalance;
}
public string GetDesignation()
{
return _designation;
}
public string GetName()
{
return _employeename;
}
}
class Program
{
static void Main(string[] args)
{
Employee objemployee = new Employee();
//Set Values
objemployee.SetName("Mr.Shyam");
objemployee.SetDesignation("Technical Book Writer");
objemployee.SetEmployeeId(775);
objemployee.SetSalary(19550);
objemployee.SetLeaveBalance(21);
//Get Values
Console.WriteLine("Employee Details is Listed Below :");
Console.WriteLine("Name: " + objemployee.GetName());
Console.WriteLine("Designation: " + objemployee.GetDesignation());
Console.WriteLine("Employee Id: " + objemployee.GetEmployeeId());
Console.WriteLine("Monthly Salary: " + objemployee.GetSalary());
Console.WriteLine("Leave Details: " + objemployee.GetLeaveBalance());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Encapsulation
{
class Employee
{
private int _employeeId;
private double _salary;
private int _leaveBalance;
private string _designation;
private string _employeename;
public int EmployeeId
{
set { _employeeId = value; }
get { return _employeeId; }
}
public double Salary
{
set { _salary = value; }
get { return _salary; }
}
public int LeaveBalance
{
set { _leaveBalance = value; }
get { return _leaveBalance; }
}
public string Designation
{
set { _designation = value; }
get { return _designation; }
}
public string EmployeeName
{
set { _employeename = value; }
get { return _employeename; }
}
}
class Program
{
static void Main(string[] args)
{
Employee objemployee = new Employee();
//Set Values
objemployee.EmployeeName = "Mr Shyam";
objemployee.EmployeeId = 775;
objemployee.Designation = "Technical Book Writer";
objemployee.Salary = 19550;
objemployee.LeaveBalance = 21;
Console.WriteLine("Employee Details Listed Below: ");
Console.WriteLine("Name : " + objemployee.EmployeeName);
Console.WriteLine("Employee Id: " + objemployee.EmployeeId);
Console.WriteLine("Designation :" + objemployee.Designation);
Console.WriteLine("Salary: " + objemployee.Salary);
Console.WriteLine("Leave Balance: " + objemployee.LeaveBalance);
}
}
} ConclusionIn this article, i have given details,how we could implement encapsulation object orinted concept. |