Encapsulation basically involves grouping all the related properties and methods that access them in an object. Whenever an application is being designed, we need to decide how many objects should be defined in it, along with the associated attributes and methods available in it.
For instance, in the car example, we have the following associated attributes and methods:
- car is an object.
- The make, model, and color are the different attributes that are present in the object.
- start, stop, and accelerate are the different methods that are present in the object.
Encapsulation allows us to achieve the following functionality in any application:
- Security: Using encapsulation, we can define our attributes in such a way that not all of the attributes of an object are exposed to the entire application. In Chapter 2, Understanding Classes, Structures, and...