Encapsulation is hiding information. Encapsulation hides implementation details from world and exposes only the contract. In case of a class, the methods exposed without private access modifier defines the contract of that class. How these methods are implemented is never known to the users of these methods.
Encapsulation can be implemented at many levels. It can be at attribute, method, class, package, module, application and even at organization level. At each level we define the boundary and hide something which is implementation. Exposed interfaces do the contract definition job.
What do we achieve by modularization?
– The main advantage of it is ease of modification. As the clients of unit are concern about the interface/contract only, the internal implementation can vary independent of client.
– This mean loose coupling amongst different units.
– Independent packaging and deployments can be looked at.
– Maintainability is imporoved.
Implementation of Encapsulation in Java:
1. Basic Encapsulation: Java provides four levels of encapsulation that can be applied at to attributes, methods and classes. The scope definer keywords private, protected, public and default (no keyword) can be used to limit the accebility.
2. Advance Encapsulation: It may be required at bit higher level than class, i.e. functional level. You may want to hide certain functionality from few of the accessors of functionality. To control this access, we may have to limit the exposure of functionality. It depends on how the functionality is exposed (by that component). Sometimes it is through implementation of public interface, allowing remote access using EJBs, Webservices, RMI etc. Encapsulation is applied here by limiting the methods allowed to access through this mechanism.
If we want to limit our discussion to object orientation only, then it is encapsulation applied to object only. But if we want to extend it to higher levels, then it can be applied to a package, module, component, application, and even enterprise. All these have certain type of encapsulation or information hiding.