- Encapsulation is a procedure of covering up of the data & functions into a single unit called as class
- An encapsulated object is often called an abstract data type.
- Encapsulation can protect your data from accidental corruption.
- Rather than defining the data in the form of public, we can declare those fields as private.
Code Example :
public class School
{
 private string Schooldepartname;
 public string SchoolDepartname
 {
  get
  {
   return Schooldepartname;
  }
  set
  {
   Schooldepartname =value;
  }
 }
}
public class Departmentmain
{
 public static int Main(string[] args)
 {
  School d= new School();
  d.SchoolDepartname="Communication";
  Console.WriteLine("The Dept. Name is :{0}",d.SchoolDepartname);
  return 0;
 }
}
  Output:The Dept. Name is : Communication
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.
Comments
Post a Comment