Encapsulation in Java

1. Encapsulation in Java is a mechanism of wrapping the data (variables) and methods together as a single unit.
2. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.

How to Achieve?

1. Declare the variables of a class as private.
2. Provide public setter and getter methods to modify and view the variables values.


1. The fields of a class can be made read-only (class which has only getter method) or write-only (class which has only setter method) .
2. A class can have total control over what is stored in its fields.

To get more details please watch below youtube video and Subscribe the channel.

No comments:

Post a Comment