Encapsulation In Java Example2 min read


Encapsulation in java is a process of wrapping code and data together into a single unit.

The Java Bean class is the example of fully encapsulated class.

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. 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.

Benefits of Encapsulation

The fields of a class can be made read-only or write-only.

A class can have total control over what is stored in its fields.

The users of a class do not know how the class stores its data. A class can change the data type of a field and users of the class do not need to change any of their code.

Leave a Comment