In Object Oriented Programming,
encapsulation is an attribute of object
design. It means that all of the object's data is contained and hidden in the object and access to it restricted to members of that class.
Programming languages aren't quite so strict and allow differing levels of access to the object's data. The first three levels of access shown below are in both C++ and C#, with the last two in C#.
- Public : All Objects can access it.
- Protected : Access is limited to members of the same class or descendants.
- Private : Access is limited to members of the same class.
- Internal : Access is limited to the current assembly.
- Protected Internal : Access is limited to the current assembly or types derived from the containing class.
For the best encapsulation, object
data should almost always be restricted to private or protected.