What is Data Encapsulation?

Data Encapsulation is an Object Oriented Programming concept that bind a group of related properties, functions, and other members are treated as a single unit. Class is the best example of Data Encapsulation. It sometimes referred to as data hiding that prevents the user to access the implementation details. Encapsulation therefore guarantees the integrity of the data contained in the Object.
What is encapsulation?
The whole idea behind the data encapsulation is to hide the implementation details from users. This is achieved through the state (the private fields) and the behaviours (the public methods) of a Class.

In the above example we use the Getname() and Setname() methods to retrieve and set student name. We use the private variable studentName and as it is not accessible directly by the user. So, in order to access this variable, we use the Getname() and Setname() methods respectively. This means that a user does not need to know how implementation takes place. Here we can see the data encapsulation is achieved through the state (the private fields) and the behaviours (the public methods) of an Object. This way only the data can accessed by public methods, by making the private fields and their implementation hidden for outside classes. That's why data encapsulation is known as data hiding.
Data Encapsulation is implemented by using access specifiers (Access Modifiers) and it defines the scope and visibility of a class member. In C# , Encapsulation uses five types of modifier to encapsulate data.
- Public
- Private
- Protected
- Internal
- Protected internal
- What is object-oriented programming?
- What is a Class?
- What is an Object?
- Constructors and Destructors
- What Is Inheritance ?
- What are the different types of inheritance ?
- What are Access Modifiers ?
- Why Classes cannot be declared as Protected?
- Can we declare private class in namespace
- Difference between Classes and Structures
- Can we use pointers in C# ?
- Why abstract class can't create instance
- Can you prevent your class from being inherited
- Difference between method Overloading and Overriding
- Difference between Early Binding and Late binding
- What is nested class
- What is partial class ?
- What is Virtual Method
- Difference between class and object
- Object Based Language and OOPs
- SOLID Principles in Object-Oriented Programming
- Solid Principles | Advantages and Disadvantages