articles

Home / DeveloperSection / Articles / Encapsulation and Access specifier

Encapsulation and Access specifier

Manish Kumar7092 16-Jan-2017


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.

classEncapsulate
        {
            int a;
            int b;
            publicvirtual voidint 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
{
  
 
    classPublic
    {
        // String Variable declared as public
        publicstring name;
        // Public method
        publicvoid print()
        {
            Console.WriteLine("\nMy name is " + name);
        }
    }
 
    classProgram
    {
        staticvoid Main(string[] args)
        {
            Public p = newPublic();
            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
{
  
 
    classPrivate
    {
        // String Variable declared as private
        //private string name;
        privatestring name;
       // protected string city;
        // Public method
        publicvoid print()
        {
            Console.WriteLine("\nMy name is " + name);
            //Console.WriteLine("\n address" + city);
        }
    }
 
    classProgram
    {
        staticvoid Main(string[] args)
        {
            Private ac = newPrivate();
        
          
            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.

 

   classPrivate
    {
        // String Variable declared as private
        //private string name;
        privatestring name;
       // protected string city;
        // Public method
        publicvoid print()
        {
            Console.WriteLine("\nMy name is " + name);
            Private p = newPrivate();
            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
{
  
 
    classProtected
    {
        // String Variable declared as Protected
        //protected string name;
        protectedstring name;
      
    }
 
    classProgram:Protected
    {
        staticvoid Main(string[] args)
        {
            Program ac = newProgram();
        
          
            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
{
  
 
    classInternal
    {
        // String Variable declared as private
        //private string name;
        internalstring name;
       // protected string city;
        // Public method
        publicvoid print()
        {
            Console.WriteLine("\nMy name is " + name);
        
            //Console.ReadLine();
 
            //Console.WriteLine("\n address" + city);
        }
    }
 
    classProgram
    {
        staticvoid Main(string[] args)
        {
            Internal ac = newInternal();
        
          
            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.       


Updated 20-Dec-2017

Leave Comment

Comments

Liked By