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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- Calci calci = new Calci();
-
- calci.num1 = 100;
- calci.num2 = 200;
- calci.Sum(33, 44);
- calci.Multiply(1, 8);
-
- Console.WriteLine();
- Console.ReadKey();
- }
- }
-
- public class Calci
- {
- public double num1;
- public double num2;
- public double result;
-
- public double Sum(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 + num2;
- return result;
- }
-
- public double Multiply(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 * num2;
- return result;
- }
- }
-
- }
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.
- using System;
-
- namespace Tutpoint
- {
- public class Program
- {
- static void Main(string[] args)
- {
- Calci calci = new Calci();
-
- calci.num1 = 100;
- calci.num2 = 200;
- calci.Sum(33, 44);
-
- Console.WriteLine();
- Console.ReadKey();
- }
- }
-
-
- private class Calci
- {
- public double num1;
- public double num2;
- public double result;
-
- public double Sum(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 + num2;
- return result;
- }
-
- }
-
- }
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.
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- Calci calci = new Calci();
-
-
-
- calci.num1 = 100;
- calci.num2 = 200;
-
-
-
- calci.Sum(33, 44);
- calci.Multiply(1, 8);
-
- Console.WriteLine();
- Console.ReadKey();
- }
- }
-
- public class Calci
- {
-
- private double num1;
-
-
- public double num2;
- public double result;
-
-
- private double Sum(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 + num2;
- return result;
- }
-
-
- public double Multiply(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 * num2;
- return result;
- }
- }
-
- }
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.
- using System;
-
- namespace Tutpoint
- {
- public class Program
- {
- static void Main(string[] args)
- {
- Calci calci = new Calci();
-
-
-
- calci.num1 = 100;
- calci.num2 = 200;
-
-
-
- calci.Sum(33, 44);
- calci.Multiply(1, 8);
-
- Console.WriteLine();
- Console.ReadKey();
- }
- }
-
- public class Calci
- {
- public class Derived
- {
- Calci calci = new Calci();
-
- public void demo()
- {
-
- calci.num1 = 100;
- calci.num2 = 200;
-
-
- calci.Sum(33, 44);
- calci.Multiply(1, 8);
- }
- }
-
- protected double num1;
-
-
- public double num2;
- public double result;
-
-
- protected double Sum(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 + num2;
- return result;
- }
-
-
- public double Multiply(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 * num2;
- return result;
- }
- }
-
- }
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
- using System;
-
- namespace Tutpoint
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Calci calci = new Calci();
-
- calci.num1 = 100;
- calci.num2 = 200;
-
-
- calci.Sum(33, 44);
- calci.Multiply(1, 8);
-
- Console.WriteLine();
- Console.ReadKey();
- }
- }
-
- internal class Calci
- {
- public class Deried
- {
- Calci calci = new Calci();
-
- public void demo()
- {
-
- calci.num1 = 100;
- calci.num2 = 200;
-
-
- calci.Sum(33, 44);
- calci.Multiply(1, 8);
- }
- }
-
- internal double num1;
-
-
- public double num2;
- public double result;
-
-
- internal double Sum(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 + num2;
- return result;
- }
-
-
- public double Multiply(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 * num2;
- return result;
- }
- }
-
- }
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.
- using System;
-
- namespace Tutpoint
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Calci calci = new Calci();
- calci.num1 = 100;
- calci.num2 = 200;
-
- calci.Sum(33, 44);
-
- Console.WriteLine();
- Console.ReadKey();
- }
- }
-
- class Calci
- {
-
- internal protected double num1;
-
-
- public double num2;
- public double result;
-
-
- protected internal double Sum(int x, int y)
- {
- num1 = x;
- num2 = y;
- result = num1 + num2;
- return result;
- }
-
- }
-
- }
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.