Sunday 7 July 2013

Encapsulation - Let's Hide

Encapsulation ...

In Simple words it means "the process of enclosing" which further means "surround completely".

Encapsulation hides the implementation details from the outside code which in turn provides high flexibility and maintainability. The classes thus written are loosely coupled and change in one class doesn't accounts for a change in some other class that access it. Due to this we can rewrite our class to append any new change without affecting any other code.

Now. How can we do that ?
  1. By keeping Instance variables protected from outside code (usually making it private)
  2. By creating accessor methods and restrict outside code from directly accessing the instance variables (use JavaBeans naming convention to create accessors).
 

No comments:

Post a Comment