Encapsulation

When designing a class, it is generally a good practice to only reveal the necessary interfaces needed to interact with the object, and hide the implementation details that are not precisely relevant. This hiding of details is known as encapsulation.

The concept of encapsulation is better enforced with the definitions of interfaces and implementations. An interface is defined by the services of communication between objects. Classes declare some methods and properties as visible to outside objects, and hide the other details. The visible, or public, methods and properties become the interface to the object. Interfaces typically only include methods, since any need to access an attribute should be made available through a public method.

public class ClsEncapsulation
{
int SqrOutput;
public ClsEncapsulation()
{
}
//Visible method
public void AssignNumber(int GivenNumber)
{
CalculateSqluare(GivenNumber);
}
public void Display()
{
Console.WriteLine("Square of given number is " + SqrOutput.ToString());
}
//Hiding Method
private void CalculateSqluare(int GnNumber)
{
SqrOutput = System.Math.Pow(GnNumber,2);
}

}

No comments: