Encapsulation is the process of wrapping up of data and members in a class. Encapsulation provides the way to hide data from the external usage. This is important in the scenario when we don't want our precious data to be used or manipulated by external user. Encapsulation is an object-oriented principle of hiding the internal state and behavior of an object, making your code more maintainable.
Advantages of Encapsulation
- Data-hiding through the access specifier private.
- Reduce coupling between objects.
- Increases the maintainability of code.
- Wrapping up of data variables and members inside the class.
The following table will give the description of the Access modifiers available in C#.
Access Modifier | Description (who can access) |
Private
| Private members can be accessed within the class where they are declared. We can't access private members outside the class, not even with the object of the class. This basically provides the concept of Data-Hiding. |
Protected | Protected members scope can be outside the class but only limited to the immediate derived class that is inheriting the base class where the protected members resides. The protected can be used by the members of same class or the members of derived class. |
Internal | Here the internal members are accessible within the same assembly. If you don't specify any modifier, by default it is of internal type.
|
Protected Internal | Here either accessed in the same assembly or the derived assembly also. This is basically a combination of protected and internal access modifiers. |
Public | Public members can be accessed anywhere. No restriction associated with this access modifier. |
Sealed Class
The sealed keyword provides the restriction that no other class in the context can inherit this class. By this, we can make our class not to be inherited by other class. If the class tries to inherit the class that is declared as sealed, the compile time error occurs.
1] Class declared as sealed
sealed class ClsSealed17 {// Any members }
Abstract Class
The class declared with abstract keyword, this means the class is only provide the essential elements and not including the background details. Abstraction is the process of including the essential elements and hiding the details. In layman terms, we can say that a computer is the abstract form, because we are not aware of the internal technicality.
If the class is declared as abstract, we have to give the body to all the methods that are declared as abstract.
1] Class declared as abstract
abstract class clsAbstractDemo{
// This can contains abstract or concrete methods.
}
2] Abstract Members
public abstract string displayname();
Note:- The abstract methods can only include the signature of the method.
Example: Demonstrate Encapsulation

ClsEncapsulation17.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace cSHARPEXAMPLES
{
class Customer
{
public void CustomerRecord()
{
AddNewRecord();
UpdateExistingRecord();
DeleteRecord();
}
protected virtual string AddNewRecord()
{ return "Base: AddNewRecord" ; } //ADD NEW RECORD
protected virtual string UpdateExistingRecord()
{ return "Base: UpdateExistingRecord" ; } //UPDATE EXISTING RECORD
protected virtual string DeleteRecord()
{ return "Base: DeleteRecord" ; } //DELETE EXISTING RECORD
}
class ClsEncapsulation17 : Customer
{
private string name = "" ; //Can't Access Outside the Class
private string address = "" ; //Can't Access Outside the Class
private string pri_Display() //Can't Access Outside the Class
{
return "This method can't be access outside the class" ;
}
public string CustomerName { //Public Property
get { return name; }
set { name = value ; }
}
public string CustomerAddress //Public property
{
get { return address; }
set { address = value ; }
}
public string CombineDisplay() //Public Method
{
return CustomerName + " :::" + CustomerAddress;
}
protected override string AddNewRecord() //Protected Method
{
return "protected method :: AddNewRecord" ;
}
protected override string UpdateExistingRecord() //Protected Method
{
return "protected method :: UpdateExistingRecord" ;
}
protected override string DeleteRecord() //Protected Method
{
base .DeleteRecord();
return "protected method :: DeleteRecord" ;
}
public string combineString()
{
string cmbStr = "" ;
cmbStr = cmbStr + AddNewRecord() + "\n" ;
cmbStr = cmbStr + UpdateExistingRecord() + "\n" ;
cmbStr = cmbStr + DeleteRecord();
return cmbStr;
}
}
}
ClsSealed17.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace cSHARPEXAMPLES
{
sealed class ClsSealed17
{
private string name = "" ;
public string CustomerName
{
get { return name; }
set { name = value ; }
}
public string displayName()
{
return name;
}
}
//Can't inherited a Sealed Class
class derivedClass //: ClsSealed17
{
}
}
ClsAbstract17.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace cSHARPEXAMPLES
{
abstract class clsAbstractDemo
{
private int _number = 10;
public string _name = "" ;
public abstract string displayname(); // Abstract method
public int calcTotal() //Concreate Method
{
return (_number * 10);
}
}
class ClsAbstract17 : clsAbstractDemo
{
public override string displayname() { //Abstract Method
return "Abstract Method Provide Body in Derived Class" ;
}
}
}
Output
Click On: Private Members Button
Click On: Public Members Button

Click On: Protected Members Button

Click On: Sealed

Click On: Abstract






