Data encapsulation is a fundamental OOPs concept that wraps the data(variables) and code(methods) as a single unit. These variables and methods can only be accessed through the current class, as they’ll be hidden from others. Encapsulation protects the data from unnecessary modification and can be achieved in two ways:
By declaring the variable of a class as private
Using public setter and getter methods to view and modify the values