Tuesday, February 16, 2010

C# Encapsulation

INTRODUCTION:

The object oriented programming will give theimpression very unnatural to a programmer with a lot of proceduralprogramming experience. In Object Oriented programming Encapsulation isthe first pace. Encapsulation is the procedure of covering up of dataand functions into a single unit (called class). An encapsulated objectis often called an abstract data type. In this article let us see aboutit in a detailed manner.

NEED FOR ENCAPSULATION:

The need of encapsulation is to protect orprevent the code (data) from accidental corruption due to the sillylittle errors that we are all prone to make. In Object orientedprogramming data is treated as a critical element in the programdevelopment and data is packed closely to the functions that operate onit and protects it from accidental modification from outside functions.

Encapsulation provides a way to protect datafrom accidental corruption. Rather than defining the data in the formof public, we can declare those fields as private. The Private data aremanipulated indirectly by two ways. Let us see some example programs inC# to demonstrate Encapsulation by those two methods. The first methodis using a pair of conventional accessor and mutator methods. Anotherone method is using a named property. Whatever be the method our aim isto use the data with out any damage or change.

ENCAPSULATION USING ACCESSORS AND MUTATORS:

Let us see an example of Department class. Tomanipulate the data in that class (String departname) we define anaccessor (get method) and mutator (set method).

using system;
public class Department
{
private string departname;
…….

// Accessor.
public string GetDepartname()
{
return departname;
}

// Mutator.
public void SetDepartname( string a)
{
departname=a;
}

}

Like the above way we can protect the privatedata from the outside world. Here we use two separate methods to assignand get the required data.

public static int Main(string[] args)
{
Department d = new Department();
d.SetDepartname("ELECTRONICS");
Console.WriteLine("The Department is :"+d.GetDepartname());
return 0;
}

In the above example we can't access theprivate data departname from an object instance. We manipulate the dataonly using those two methods.

ENCAPSULATION USING PROPERTIES:

Properties are a new language featureintroduced with C#. Only a few languages support this property.Properties in C# helps in protect a field in a class by reading andwriting to it. The first method itself is good but Encapsulation can beaccomplished much smoother with properties.Now let's see an example.

using system;
public class Department
{
private string departname;
public string Departname
{
get
{
return departname;
}
set
{
departname=value;
}
}
}
public class Departmentmain
{
public static int Main(string[] args)
{
Department d= new Department();
d.departname="Communication";
Console.WriteLine("The Department is :{0}",d.Departname);
return 0;
}
}

From the above example we see the usage ofEncapsulation by using properties. The property has two accessor getand set. The get accessor returns the value of the some property field.The set accessor sets the value of the some property field with thecontents of "value�. Properties can be made read-only. This isaccomplished by having only a get accessor in the propertyimplementation.

READ ONLY PROPERTY:

using system;
public class ReadDepartment
{
private string departname;
public ReadDepartment(string avalue)
{
departname=avalue;
}
public string Departname
{
get
{
return departname;
}
}
}
public class ReadDepartmain
{
public static int Main(string[] args)
{
ReadDepartment d= new ReadDepartment("COMPUTERSCIENCE");
Console.WriteLine("The Department is: {0}",d.Departname);
return 0;
}
}

In the above example we see how to implement aread-only property. The class ReadDepartment has a Departname propertythat only implements a get accessor. It leaves out the set accessor.This particular class has a constructor, which accepts a stringparameter. The Main method of the ReadDepartmain class creates a newobject named d. The instantiation of the d object uses the constructorof the ReadDepartment that takes a string parameter. Since the aboveprogram is read-only, we cannot set the value to the field departnameand we only read or get the value of the data from the field.Properties can be made also Write-only. This is accomplished by havingonly a set accessor in the property implementation.

WRITE ONLY PROPERTY:

using system;
public class WriteDepartment
{
private string departname;

public string Departname
{
set
{
departname=value;
Console.WriteLine("The Department is :{0}",departname);
}
}
}
public class WriteDepartmain
{
public static int Main(string[] args)
{
WriteDepartment d= new WriteDepartment();
d.departname="COMPUTERSCIENCE";
return 0;
}
}

In the above example we see how to implement aWrite-only property. The class WriteDepartment has now has a Departnameproperty that only implements a set accessor. It leaves out the getaccessor. The set accessor method is varied a little by it prints thevalue of the departname after it is assigned.

Conclusion:

The Encapsulation is the first footsteptowards the object-oriented programming. This article gives you alittle bit information about Encapsulation. Using accessor and mutatormethods we can make encapsulation. Another one method is using a namedproperty. The benefit of properties is that the users of your objectsare able to manipulate the internal data point using a single nameditem.

Related Articles :

No comments:

Post a Comment