Encapsulation is grouping the related methods, variables, and properties in a single unit. It is used for protecting the objects and methods of one class from getting accessed by another class. In short, it is hiding the inner parts of the machine from the outside world.
namespace Encapsulation
{
//Encapsulating the methods of Testers class.
class Testers
{
//applying access modifiers on methods
private void Print() //private access modifier applied
{
Console.WriteLine("Testing..");
}
protected void Test()//protected access modifier applied
{
Console.WriteLine("Test2..");
}
protected internal void Tester()//protected internal access modifier applied
{
Console.WriteLine("Test3..");
}
}
class Program : Testers
{
static void Main(string[] args)
{
Program obj = new Program(); //Instantiating Program class
//Trying to call methods of testers class
//obj.Print();
obj.Test();
obj.Tester();
Console.ReadLine();
}
}
}
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Encapsulation is grouping the related methods, variables, and properties in a single unit. It is used for protecting the objects and methods of one class from getting accessed by another class. In short, it is hiding the inner parts of the machine from the outside world.