Encapsulation And Access Specifiers In C#

In this blog, I am going to talk about Encapsulation and Access Specifiers in C#.
 
Why we should use encapsulation
 
Whenever a program or project is developed, data security and privacy is the main concern for a user before using it. From a developer's or programmer's perspective, code misuse or access and modification of the code/data from unnecessary sources or methods call are the main concerns. Encapsulation is one of the best ways to stop this.
 
Encapsulation
  • Encapsulation is one of the most important features of an object-oriented language.
  • Encapsulation is a process of isolating the code/data from direct access by implementing access specifiers with it.
  • Encapsulation is performed to prevent the code/data from unnecessary modification, from an unauthorized user and protect the data/code from getting corrupt.
  • Encapsulation is a process of enclosing the data, functions into a single logical unit called class.
  • Encapsulation is a way of hiding the class members from outside classes.
  • It is called as Information hiding also.
Access Specifiers
 
Access Specifiers are used in defining the scope and visibility of a class and its members. Access Specifiers are of the following types,
  • Public
  • Private
  • Protected
  • Internal
  • Protected Internal
Public
 
a) 'public' keyword is used to declare a class or class members with public access specifiers.
b) A public class or public class members can be used anywhere in the code. The public class or Public members can be accessed outside their class.
c) Classes and structs declared directly inside namespace can be public or internal. If no access modifier is specified, then they are set to 'internal' by default.
d) Class members or struct members are by default private if no access modifier is specified.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Calci calci = new Calci();  
  10.   
  11.             calci.num1 = 100;  
  12.             calci.num2 = 200;  
  13.             calci.Sum(33, 44);  
  14.             calci.Multiply(1, 8);  
  15.   
  16.             Console.WriteLine();  
  17.             Console.ReadKey();  
  18.         }  
  19.     }  
  20.   
  21.     public class Calci  
  22.     {  
  23.         public double num1;  
  24.         public double num2;  
  25.         public double result;  
  26.   
  27.         public double Sum(int x, int y)  
  28.         {  
  29.             num1 = x;  
  30.             num2 = y;  
  31.             result = num1 + num2;  
  32.             return result;  
  33.         }  
  34.   
  35.         public double Multiply(int x, int y)  
  36.         {  
  37.             num1 = x;  
  38.             num2 = y;  
  39.             result = num1 * num2;  
  40.             return result;  
  41.         }  
  42.     }  
  43.   
  44. }  
In the program above, two public classes 'Program' and 'Calci' are created. 'Calci' class contains all public members. In the 'Main' method, after creating the object of 'Calci' class, we can access all the public members. You may be wondering what will happen when we change the access specifier of 'Calci' class to private? If we do so, then the compiler will generate an error as "Elements defined in a namespace cannot be explicitly declared as private, protected or protected internal" as shown in the below program.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     public class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Calci calci = new Calci();  
  10.   
  11.             calci.num1 = 100;  
  12.             calci.num2 = 200;  
  13.             calci.Sum(33, 44);  
  14.   
  15.             Console.WriteLine();  
  16.             Console.ReadKey();  
  17.         }  
  18.     }  
  19.   
  20.     // Compiler will generate an error as "Elements defined in a namespace cannot be explicitly declared as private, protected or protected internal"  
  21.     private class Calci  
  22.     {  
  23.         public double num1;  
  24.         public double num2;  
  25.         public double result;  
  26.   
  27.         public double Sum(int x, int y)  
  28.         {  
  29.             num1 = x;  
  30.             num2 = y;  
  31.             result = num1 + num2;  
  32.             return result;  
  33.         }  
  34.   
  35.     }  
  36.   
  37. }  
Private
 
a) 'private' keyword is used to declare a class or class members with private access specifiers.
b) Private members of a class can only be accessed within the class. They cannot be accessed outside the class. In other words, Private members of a class are hidden outside the class.
c) Classes and structs declared directly inside namespace cannot be private. They can only be public or internal.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Calci calci = new Calci();  
  10.   
  11.             // variable 'num1' of class 'Calci' is inaccessible because it is declared with private and we are trying to access it outside the class  
  12.             // Compiler will generate an error as "'calci.num1' is inaccessible due to its protection level"  
  13.             calci.num1 = 100;  
  14.             calci.num2 = 200;  
  15.   
  16.             // Method 'Sum' of class 'Calci' is inaccessible because it is declared with private and we are trying to access it outside the class  
  17.             // Compiler will generate an error as "'calci.Sum(int,int)' is inaccessible due to its protection level"  
  18.             calci.Sum(33, 44);  
  19.             calci.Multiply(1, 8);  
  20.   
  21.             Console.WriteLine();  
  22.             Console.ReadKey();  
  23.         }  
  24.     }  
  25.   
  26.     public class Calci  
  27.     {  
  28.         //Variable with private access specifier  
  29.         private double num1;  
  30.   
  31.         //Variables with public access specifier  
  32.         public double num2;  
  33.         public double result;  
  34.   
  35.         //Method with private access specifier  
  36.         private double Sum(int x, int y)  
  37.         {  
  38.             num1 = x;  
  39.             num2 = y;  
  40.             result = num1 + num2;  
  41.             return result;  
  42.         }  
  43.   
  44.         //Method with public access specifier  
  45.         public double Multiply(int x, int y)  
  46.         {  
  47.             num1 = x;  
  48.             num2 = y;  
  49.             result = num1 * num2;  
  50.             return result;  
  51.         }  
  52.     }  
  53.   
  54. }  
In the program above, two public classes 'Program' and 'Calci' are created. 'Calci' class contains private and public members. In the 'Main' method, after creating the object of 'Calci' class, we can access only the public members. When we try to access the private members outside the class then compiler will generate an error as "Element is inaccessible due to its protection level".
 
Protected
 
a) 'Protected' keyword is used to declare a class or class members with Protected access specifiers.
b) Protected members of a class can only be accessed within the class or in child/derived classes.
c) Classes and structs declared directly inside namespace cannot be Protected. They can only be public or internal.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     public class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Calci calci = new Calci();  
  10.   
  11.             // variable 'num1' of class 'Calci' is inaccessible because it is declared with protected and we are trying to access it outside the class  
  12.             // Compiler will generate an error as "'calci.num1' is inaccessible due to its protection level"  
  13.             calci.num1 = 100;  
  14.             calci.num2 = 200;  
  15.   
  16.             // Method 'Sum' of class 'Calci' is inaccessible because it is declared with protected and we are trying to access it outside the class  
  17.             // Compiler will generate an error as "'calci.Sum(int,int)' is inaccessible due to its protection level"  
  18.             calci.Sum(33, 44);  
  19.             calci.Multiply(1, 8);  
  20.   
  21.             Console.WriteLine();  
  22.             Console.ReadKey();  
  23.         }  
  24.     }  
  25.   
  26.     public class Calci  
  27.     {  
  28.         public class Derived  
  29.         {  
  30.             Calci calci = new Calci();  
  31.   
  32.             public void demo()  
  33.             {  
  34.                 // Variable 'num1' is accessible as 'derived' is a child class of class 'Calci'  
  35.                 calci.num1 = 100;  
  36.                 calci.num2 = 200;  
  37.   
  38.                 // Method 'Sum' is accessible as 'derived' is a child class of class 'Calci'  
  39.                 calci.Sum(33, 44);  
  40.                 calci.Multiply(1, 8);  
  41.             }  
  42.         }  
  43.         //Variable with protected access specifier  
  44.         protected double num1;  
  45.   
  46.         //Variables with public access specifier  
  47.         public double num2;  
  48.         public double result;  
  49.   
  50.         //Method with protected access specifier  
  51.         protected double Sum(int x, int y)  
  52.         {  
  53.             num1 = x;  
  54.             num2 = y;  
  55.             result = num1 + num2;  
  56.             return result;  
  57.         }  
  58.   
  59.         //Method with public access specifier  
  60.         public double Multiply(int x, int y)  
  61.         {  
  62.             num1 = x;  
  63.             num2 = y;  
  64.             result = num1 * num2;  
  65.             return result;  
  66.         }  
  67.     }  
  68.   
  69. }  
In the program above, Three public classes 'Program', 'Calci' and 'Derived' are created. Class 'Derived' is the child class of class 'Calci'. So Protected members of class 'Calci' can also be accessible to the child class but inaccessible for class 'Program'. When we try to access the Protected members in class 'Calci' then the compiler will generate an error as "Element is inaccessible due to its protection level".

Internal
 
a) 'Internal' keyword is used to declare a class or class members with Internal access specifiers.
b) Internal members of a class can be accessed anywhere within the assembly. In other words, they can be used in any class throughout the assembly. Outside the assembly or application, they are not accessible.
c) Classes and structs declared directly inside namespace can be public or internal. If no access modifier is specified then they are 'internal' by-default
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     internal class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Calci calci = new Calci();  
  10.             // Variable 'num1' is accessible as internal members can be accessed anywhere with in assembly  
  11.             calci.num1 = 100;  
  12.             calci.num2 = 200;  
  13.   
  14.             // Method 'Sum' is accessible as internal members can be accessed anywhere with in assembly  
  15.             calci.Sum(33, 44);  
  16.             calci.Multiply(1, 8);  
  17.   
  18.             Console.WriteLine();  
  19.             Console.ReadKey();  
  20.         }  
  21.     }  
  22.   
  23.     internal class Calci  
  24.     {  
  25.         public class Deried  
  26.         {  
  27.             Calci calci = new Calci();  
  28.   
  29.             public void demo()  
  30.             {  
  31.                 // Variable 'num1' is accessible as internal members can be accessed anywhere with in assembly  
  32.                 calci.num1 = 100;  
  33.                 calci.num2 = 200;  
  34.   
  35.                 // Method 'Sum' is accessible as internal members can be accessed anywhere with in assembly  
  36.                 calci.Sum(33, 44);  
  37.                 calci.Multiply(1, 8);  
  38.             }  
  39.         }  
  40.         //Variable with internal access specifier  
  41.         internal double num1;  
  42.   
  43.         //Variables with public access specifier  
  44.         public double num2;  
  45.         public double result;  
  46.   
  47.         //Method with internal access specifier  
  48.         internal double Sum(int x, int y)  
  49.         {  
  50.             num1 = x;  
  51.             num2 = y;  
  52.             result = num1 + num2;  
  53.             return result;  
  54.         }  
  55.   
  56.         //Method with public access specifier  
  57.         public double Multiply(int x, int y)  
  58.         {  
  59.             num1 = x;  
  60.             num2 = y;  
  61.             result = num1 * num2;  
  62.             return result;  
  63.         }  
  64.     }  
  65.   
  66. }  
In the program above, All the class members declared with 'internal' access specifier are accessible anywhere throughout the assembly or application.

Protected Internal
 
a) 'Protected Internal' or 'Internal Protected' is used to declare a class or class members with Protected Internal access specifiers.
b) Protected Internal members of a class can be accessed anywhere within the assembly or in derived/child classes of other assemblies.
c) Classes and structs declared directly inside namespace cannot be protected internal. They can only be public or internal.
d) It is the combination of protected OR internal.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     internal class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Calci calci = new Calci();  
  10.             calci.num1 = 100;  
  11.             calci.num2 = 200;  
  12.   
  13.             calci.Sum(33, 44);  
  14.   
  15.             Console.WriteLine();  
  16.             Console.ReadKey();  
  17.         }  
  18.     }  
  19.   
  20.     class Calci  
  21.     {  
  22.         //Variable with 'internal protected' access specifier  
  23.         internal protected double num1;  
  24.   
  25.         //Variables with public access specifier  
  26.         public double num2;  
  27.         public double result;  
  28.   
  29.         //Method with 'protected internal' access specifier  
  30.         protected internal double Sum(int x, int y)  
  31.         {  
  32.             num1 = x;  
  33.             num2 = y;  
  34.             result = num1 + num2;  
  35.             return result;  
  36.         }  
  37.   
  38.     }  
  39.   
  40. }  
Conclusion
 
Encapsulation is one of the primary aspects of object-oriented language. Encapsulation prevents the code/data from unnecessary access and defines scope of members. We should declare critical members and variables with proper access modifiers to avoid unnecessary modification. I hope this article helps you to understand a bit more about Encapsulation and Access Specifiers.
 
Thank you. Please feel free to ask any question or make a suggestion.
Ebook Download
View all
Learn
View all