encapsulation is defined as "enclosing one or more items in a physical or logical package". In the object-oriented programming methodology, encapsulation is designed to prevent access to the implementation details.
Abstraction and encapsulation are the related features of object-oriented programming. Abstraction allows for the visualization of related information, and encapsulation enables the programmer to achieve the desired level of abstraction .
Encapsulation is implemented by using access modifiers . An access modifier defines the scope and visibility of a class member. The access modifiers supported by C # are as follows:
- Public
- Private
- Protected
- Internal
- Protected Internal
1 , Public access Modifiers
The public access modifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed by an external class.
2 , Private access Modifiers
The Private access modifier allows a class to hide its member variables and member functions from other functions and objects. Only a function in the same class can access its private members. Even an instance of a class cannot access its private members. 3 , Protected access Modifiers
The Protected access modifier allows subclasses to access the member variables and member functions of its base class. This helps to implement inheritance. We will discuss this in detail in the inherited chapters. Discuss this in more detail.
4.Internal access modifier
The Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current program. In other words, any member with the internal access modifier can be defined to be accessed by any class or method within the application defined by that member.
5 , Protected Internal access Modifiers
The Protected Internal access modifier allows a class to hide its member variables and member functions from other class objects and functions other than subclasses within the same application. This is also used to implement inheritance.
C # access Modifiers