Encapsulation, also known as information hiding, Is the procedure of covering up of data and functions into a single class. Encapsulation is used to prevent the user of a given class from manipulating objects in ways that are not intended by the designer. This can be achieved by restricting access to the members. Unprotected data is vulnerable to accidental corruption due to the errors in other parts of program.
Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. Those are ways to specify access in C#:
- Public: Access to all code in the program
- Private: Access to only members of the same class
- Protected: Access to members of same class and its derived classes
- Internal: Access to current assembly
- Protected Internal: Access to current assembly and types derived from containing class
Public Access Specifiers
Public class member can be accessed by other class member that is initialized outside the class. A public member can be accessed from anywhere even outside the namespace. Public access specifier allows a class to expose its member variables and member functions to other functions and objects, this way methods can be called from outside of the class with declaration.
using System; class PublicAccess { // Public string public string word; // Public method public void print() { Console.WriteLine("Selected word: " + word); } } //Other Class class Program { static void Main(string[] args) { //Creating instance of the object PublicAccess ac = new PublicAccess(); Console.Write("Chose word:"); // Calling methods form instance ac.word = Console.ReadLine(); ac.print(); Console.ReadLine(); } }
In this example user can change the value of the string variable and can call method from instance of the object.
Private Access Specifiers
Changing variable specifier to private restricts its accessibility , by hiding it from other classes. After changing string variable from first example to private we an no longer call its name in the instance.
private string word;
//here we will have a compiler error ac.word //We can still call our method ac.print();
This example shows how to restrict access to variables and prevent other classes from accidentally changing it value.
Protected Access Specifiers
Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance. You can read my article about Inheritance, here.
Now, in our example we change the specifier to protected.
protected string word;
The outcome is the same as in private version. But now we can create class that inherit from main class, and we can change value of the variable from there.
using System; //New class that inherits from previous class class Program : PublicAccess { static void Main(string[] args) { Program p=new Program(); Console.Write("Chose Word:"); p.name = Console.ReadLine(); p.print(); Console.ReadLine(); } }
Now we don’t have any errors, and we can change the value of the variable
Internal Access Specifier
It is the default access specifiers for a class in C# programming. With this property any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.
In our example output is the same as in public variable, because both classes exists in same application.
Protected Internal Access
The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application. The protected internal access specifier allows its members to be accessed in derived class, containing class or classes within same application. This the type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
In other words the type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.
In this article I presented you all possible implementations of encapsulation. This concept goes well together with inheritance. Protecting variables can prevent a code from corruption and save many hour of debugging. In next article I’ll present last major principle of object oriented programming, abstraction. Have fun with this new concepts. Stay Awesome!