Encapsulation:

Encapsulation(Information hiding) is the feature of hiding all the internal details of object from the outside world.It Encapsulate like(capsule) all the data member and method in a single unit.It means that group of related properties ,method and other member are kept into a single unit.A Class act as a container or capsule to encapsulate method,properties and data member.

class Encapsulate
        {
            int a;
            int b;
            public virtual void int balance()
            {
            }
        }


In above class I have encapsulated variable and method in a single unit.

Encapsulation is implemented by access specifier.

Access Specifier:-Access specifier define the scope of accessibility of an object and its member.

Public

Private

Internal

Protected

Protected Internal

Public Access Specifier

Public access specifier  make his member variable and method usable for another class.Any Other Class Can access the public member.A Public member can be accessed from any where even outside the namespace.

using System;
 
 
namespace ConsoleApplication2
{
  
 
    class Public
    {
        // String Variable declared as public
        public string name;
        // Public method
        public void print()
        {
            Console.WriteLine("\nMy name is " + name);
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            Public p = new Public();
            Console.Write("Enter your name:\t");
            // Accepting value in public variable that is outside the class
            p.name = Console.ReadLine();
            p.print();
 
            Console.ReadLine();
        }
    }
}
 
Private Access Specifier:-

It hide member variable and member function from outside the class. If we declare any member as private then we can not acces from outside class.It’s accessibility is limited only inside the same class.If we try to access those member by making its instance then also we can not access private member.*Only function of the same class can access its private members.

using System;
 
 
namespace ConsoleApplication2
{
  
 
    class Private
    {
        // String Variable declared as private
        //private string name;
        private string name;
       // protected string city;
        // Public method
        public void print()
        {
            Console.WriteLine("\nMy name is " + name);
            //Console.WriteLine("\n address" + city);
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            Private ac = new Private();
        
          
            Console.Write("Enter your name:\t");
            // Accepting value in public variable that is outside the class
           // ac.name = Console.ReadLine();
            //ac.address = Console.ReadLine();
            ac.name = Console.ReadLine();
            ac.print();
 
            Console.ReadLine();
           
        }
    }
}
 
 
                   }


In the above example I have declare address as private so when we try to access by


its class instance it shows error..error is inaccessible due to its protection level.


We can access private member in the same class by making its object like this.

 

    class Private
    {
        // String Variable declared as private
        //private string name;
        private string name;
       // protected string city;
        // Public method
        public void print()
        {
            Console.WriteLine("\nMy name is " + name);
            Private p = new Private();
            p.name = Console.ReadLine();
            p.print();
            //Console.ReadLine();
 
            //Console.WriteLine("\n address" + city);
        }
    }
 
 Protected Access Specifier:-  


It allows to access its function and properties in same class and its base class.It is the second protective specifier after private specifier.For accesing protected member we have to inherite the parent class in the base  class.  

      


  using System; 
 
namespace ConsoleApplication2
{
  
 
    class Protected
    {
        // String Variable declared as Protected
        //protected string name;
        protected string name;
      
    }
 
    class Program:Protected
    {
        static void Main(string[] args)
        {
            Program ac = new Program();
        
          
            Console.Write("Enter your name:\t");
            // Accepting value in protected variable that is outside the class
           ac.name = Console.ReadLine();
 
            ac.print();
 
            Console.ReadLine();
           
        }
    }
}

 

 

In the above example I have inhirited the parent class Protected in the base class


Program then we get the permission to access the protected member.    

Internal :-

This access specifier access limitation in another assembly. Internal can be access in the whole application  in the same assembly.But it can not be access in another assembly .

using System; 
 
namespace ConsoleApplication2
{
  
 
    class Internal
    {
        // String Variable declared as private
        //private string name;
        internal string name;
       // protected string city;
        // Public method
        public void print()
        {
            Console.WriteLine("\nMy name is " + name);
        
            //Console.ReadLine();
 
            //Console.WriteLine("\n address" + city);
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            Internal ac = new Internal();
        
          
            Console.Write("Enter your name:\t");
            // Accepting value in internal variable that is outside the class
           ac.name = Console.ReadLine();
            //ac.address = Console.ReadLine();
           // ac.name = Console.ReadLine();
 
            ac.print();
 
            Console.ReadLine();
           
        }
    }
}
Protected Internal:-
Protected Interanl specifier is access in the whole application in the same


assembly and in derived class of another assembly.        

  Modified On Dec-20-2017 11:50:42 PM

Leave Comment