Encapsulation in its simple terms refers to information hiding. In software development, encapsulating all the methods and classes into a module presents an interface which can be used for further development and can also be re-used. If any change requires, the change is restricted to a small subset of entire program thus saving time and cost of development.
Here am going to provide a small example of how classes are used in c# for encapsulation.
class Program
{
public void method1()
{
Console.Write("method1 called ");
}
public void method2()
{ }
static void Main(string[] args)
{
Class2 c2 = new Class2();
c2.method3();
}
}
class Class1
{
public Program prog()
{
return new Program();
}
}
class Class2
{
public void method3()
{
Class1 c = new Class1();
c.prog().method1();
}
}
In this small example, method3() of class2 is used to call method1() of class Program through class1's method prog() which in turn returns a Program instance.
No comments:
Post a Comment