Encapsulation And Access Modifiers In Java
The single most important factor that distinguishes a
well-designed module from a
poorly designed one is the degree to which the module
hides its internal data and
other implementation details from other modules. A
well-designed module hides
all of its implementation details, cleanly separating its
API from its implementation.
Modules then communicate only through their APIs and are
oblivious to
each others’ inner workings. This concept, known as information
hiding or encapsulation,
is one of the
fundamental tenets of software design.
Importance of Encapsulation or Information Hiding in Software World:
- It decouples the modules that comprise a system
- it decrease the time of testing modules.
- it decreases maintenance cost
- easily understood the modules
- it speeds up system development because the modules are developed in parallel.
- can be understood more quickly and debugged with little fear of harming others modules.
Private: The member is accessible only from the top level class where it is declared.
package-private: the member is accessible from any class within the package known as default access, this the access level you get when no access modifier is specified
Protected: the member is accessible from subclasses of the class where it is declared and from any class withing the package.
Public: the member is accessible from anywhere
