8.
Encapsulation in Java
Encapsulation in the world of objects, means keeping an object's inner workings hidden while
showing how it can be used. This provides a form of abstraction.
Encapsulation is defined as the wrapping up of data under a single unit.
We can perform encapsulation in java with the help of access specifiers which restricts the
access as per our requirement.
Access specifiers (or access modifiers) control the visibility of classes, methods, and variables.
There are four main access specifiers:
1) private: Members declared as private are accessible only within the same class. They
are not visible to other classes, even those in the same package.
2) protected: Members declared as protected are accessible within the same package and
also by subclasses (even if they are in different packages).
3) default: If no access modifier is specified, the member is considered to have "default"
access. It is accessible only within its own package.
4) public: Members (classes, methods, variables) declared as public are accessible from
any other class in any package.
Image Source: Medium
Now, one of the most asked questions is why do we require encapsulation?
A.Encapsulation offers several key benefits:
A)Data Integrity: It protects an object's internal data from unintended access or modification,
ensuring consistent state and preventing invalid data.
B)Information Hiding: By concealing internal details, encapsulation simplifies the interface,
reducing complexity and making maintenance easier. This allows for implementation changes
without impacting the broader system.
C)Data Abstraction: It represents complex data structures in a simplified way, facilitating easier
access and manipulation.
D)Modularity and Reusability: Encapsulation promotes modular design, allowing for reusable
components and reducing code duplication, which enhances maintainability.
To understand the benefits of encapsulation, take a look at the following scenario:
Suppose we have a class called BankAccount that represents a bank account with a balance
and two methods for depositing and withdrawing money.
If we implement this class without encapsulation like the program below, we might define the
balance field as public, allowing external entities to modify the balance directly. This would
create a security risk since any entity could change the balance without authorization.
See the output of the
following code. The balance
was directly modified in L20
which led to the wrong
amount being populated in
the application. This leads to
poor security of the program.
Instead, we can use encapsulation to hide the internal details of the BankAccount class and
provide a public interface for accessing and modifying the account balance. We can define the
balance field as private and provide two public methods called deposit and withdraw, which
modify the balance in a controlled manner. This ensures that only authorized entities can modify
the balance and that the account remains valid.
See the code below:
See the output which is giving a correct answer as
intended.
Thus, using encapsulation, we prevented the un-
authorized access to the variable and forced the use
of the public getter and setter methods where we
can add the conditions for checking the validity of
the inputs.