Encapsulation in C# - OOPS concept
The wrapping up of data and functions into a single unit (called class) is known as an encapsulation.
Encapsulation is
- Hiding complexity.
- Binding the data and the function together.
- Making complicated methods private.
- Making an instance variables private.
- Hiding an unnecessary data and the functions from the end user.
A class can be public or an internal but not a private, protected or protected internal.
Why a class cannot be private or protected
A protected declaration at the namespace level is meaningless. Protected for, or from, what? Access from other namespaces i.e. what 'internal' does.
A namespace has no class hierarchy. Can you inherit from a namespace? No.
Nested class can have all access modifiers.
- class parent {
- private class Child1 {}
- protected class Child2 {}
- protected internal class Child3 {}
- }
Note
I have created a console Application and a class library (i.e. 2 assemblies) to show the difference between the 5 access modifiers in C# to achieve an encapsulation
private
- namespace ConsoleApplication1 {
- public class demo {
- private string name = "some name";
- }
- public class Program: demo {
- static void Main(string[] args) {
- demo d = new demo();
- d.name
- ReadLine();
- }
- }
- }
public
- namespace ConsoleApplication1 {
- public class demo {
- public string name = "some name";
- }
- public class Program: demo {
- static void Main(string[] args) {
- demo d = new demo();
- WriteLine(d.name);
- ReadLine();
- }
- }
- }
Protected
Protected is only accessible in the same class and in the derived class. This supports cross assembly i.e. if our class is in one assembly and we derive it in another assembly’s class, then we can have access to the protected access modifier members.
Scenario 1 - within same assembly
- namespace ConsoleApplication1 {
- public class Demo {
- protected string name = "demo";
- }
- public class Program: Demo {
- static void Main(string[] args) {
- Demo d = new Demo();
- WriteLine(d.name);
- Program p = new Program();
- WriteLine(p.name);
- ReadLine();
- }
- }
- }
Scenario 2 - with cross assembly
- namespace ClassLibrary1 {
- public class Demo {
- protected string name = "demo";
- }
- }
It is another assembly, where we are going to inherit it and try to use the variable.
- namespace ConsoleApplication1 {
- public class Program: Demo {
- static void Main(string[] args) {
- Demo d = new Demo();
- WriteLine(d.name);
- Program p = new Program();
- WriteLine(p.name);
- ReadLine();
- }
- }
- }
Internal
Scenario 1 - within same assembly
- namespace ConsoleApplication1 {
- public class Demo {
- internal string name = "demo";
- }
- public class Program: Demo {
- static void Main(string[] args) {
- Demo d = new Demo();
- WriteLine(d.name);
- ReadLine();
- }
- }
- }
Scenario 2 - with different assembly
- First assembly
- namespace ClassLibrary1 {
- public class Demo {
- internal string name = "demo";
- }
- }
Second assembly
- namespace ConsoleApplication1 {
- public class Program: Demo {
- static void Main(string[] args) {
- Demo d = new Demo();
- WriteLine(d.name);
- ReadLine();
- }
- }
- }
Protected Internal
Scenario 1 - within same assembly
- namespace ConsoleApplication1 {
- public class Demo {
- protected internal string name = "demo";
- }
- public class Program: Demo {
- static void Main(string[] args) {
- Program d = new Program();
- WriteLine(d.name);
- ReadLine();
- }
- }
- }
Scenario 2 - with different assembly
- First assembly
- namespace ClassLibrary1 {
- public class Demo {
- protected internal string name = "demo";
- }
- }
- Second assembly
- namespace ConsoleApplication1 {
- public class Program: Demo {
- static void Main(string[] args) {
- Program d = new Program();
- WriteLine(d.name);
- ReadLine();
- }
- }
- }