Thursday, March 10, 2016

Encapsulation in Java-1

Encapsulation in Java-1


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

e.g. capsule i.e mixed of several medicines.



We can create a fully encapsulated class by making all the data members of the
class private. Now we can use setter and getter methods to set and get the data in it.

Java Bean is the example of fully encapsulated class.

Advantage of Encapsulation

By providing only setter or getter method, you can make the class read-only or write-only.
It provides you the control over the data. Suppose you want to set the value of id
i.e. greater than 100 only, you can write the logic inside the setter method.

No comments:

Post a Comment