What is Data Encapsulation?
Data encapsulation is a fundamental concept in Object-Oriented Programming (OOP) that combines a cohesive set of properties, functions, and other members into a unified entity. A class serves as a prime illustration of data encapsulation, exemplifying the concept in practice. It is occasionally denoted as data hiding, as it restricts user access to the inner workings and implementation details. By encapsulating data within an object, the integrity and reliability of the contained information are assured, promoting robustness and data security.
What is encapsulation?
The primary objective of data encapsulation is to shield the intricate implementation details of a class from users. This objective is accomplished by using the state, represented by private fields, and the behaviors, encompassed by public methods, of the class. By encapsulating data within private fields and exposing only the necessary functionalities through public methods, users are insulated from the inner workings of the class. This encapsulation maintains a clear separation between the implementation and the usage, promoting better code organization, maintainability, and abstraction.
In the given example, the Getname() and Setname() methods are utilized to retrieve and modify the student name. The studentName variable, designated as private, cannot be accessed directly by the user. Instead, the Getname() and Setname() methods are employed to interact with this variable. As a result, users are shielded from the underlying implementation details. This exemplifies the accomplishment of data encapsulation through the utilization of private fields (state) and public methods (behaviors) within an object. By concealing the private fields and their implementation from external classes, data encapsulation ensures that only the desired data can be accessed through public methods, thereby justifying the term "data hiding."
Data encapsulation is effectively implemented through the use of access specifiers, commonly known as access modifiers, which define the scope and visibility of class members. In C#, there are five types of modifiers employed in encapsulating data, enabling developers to control the accessibility and exposure of class members.
- Public
- Private
- Protected
- Internal
- Protected internal
You can see here more details about... . Access Specifiers
- 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 C#
- Solid Principles | Advantages and Disadvantages