---
title: "Encapsulation and Access specifier"  
description: "Encapsulation(Information hiding) is the feature of hiding all the internal details of object from the outside world."  
author: "Manish Kumar"  
published: 2017-01-16  
updated: 2017-12-20  
canonical: https://www.mindstick.com/articles/12229/encapsulation-and-access-specifier  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# Encapsulation and Access specifier

\

##### Encapsulation:

[Encapsulation](https://www.mindstick.com/articles/11911/encapsulation)([Information hiding](https://www.mindstick.com/forum/1050/abstraction-vs-information-hiding-vs-encapsulation)) 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](https://yourviews.mindstick.com/view/81845/now-it-s-possible-to-predict-properties-of-any-molecule) ,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](https://www.mindstick.com/interview/209/describe-the-accessibility-modifier-protected-internal) 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](https://www.mindstick.com/interview/2711/what-if-the-main-method-is-declared-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](https://answers.mindstick.com/qa/110885/what-is-the-indian-parliament-private-member-s-bill).*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](https://www.mindstick.com/forum/12939/access-private-method-another-class-using-constructor-in-java) 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](https://www.mindstick.com/blog/116/base-class-initilization).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](https://www.mindstick.com/articles/12824/calculator-application-in-android) 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](https://answers.mindstick.com/qa/30686/what-is-a-base-class-and-derived-class) of another assembly.

---

Original Source: https://www.mindstick.com/articles/12229/encapsulation-and-access-specifier

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
