What is Encapsulation?

Shikhar Arora

Total Post:6

Points:42

Posted by  Shikhar Arora
C#
 623  View(s)
Ratings:
Rate this:
What is Encapsulation C#?
  1. Re: What is Encapsulation?

    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();
            }
        }
    }

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.