Monday, 21 February 2011

What is encapsulation


Encapsulation:

Encapsulation is the process of protecting /hiding data, by declaring variables as private and providing public setter and getter method.

(or)

Wrapping or binding variables of a class with methods which operated on those variables is called as Encapsulation.

1) If it is needed how can we give access to outside member?

By using public methods. Declare variables as private and access them into public methods with in the class, call this public methods form outside the class.

2) What is the difference between in accessing variables directly and via public setter and getter methods?

If you allow user to access variables via method, we can validate by authentication logic if required.

This the main reason behind encapsulation, suggesting us to protect variables with private access specifier and provide the accessibility via methods.

No comments:

Post a Comment