11.5 Structure of Classes and Objects in Java: Encapsulation
In Java, the concept of encapsulation is one of the fundamental pillars of object-oriented programming. Encapsulation is the technique of hiding the implementation details of a class and exposing only the necessary functionality to the outside world. This is done using access control to members of a class, which are mainly variables and methods. Encapsulation helps protect data and maintain object integrity, and makes code easier to maintain and evolve.
Encapsulation in Java is achieved through access modifiers, which control the visibility of a class's variables and methods. The four access modifiers in Java are:
- private: The member can only be accessed within the class itself.
- default (no modifier): The member can be accessed within the same package.
- protected: The member can be accessed within the same package or by subclasses in different packages.
- public: The member can be accessed from anywhere.
To better understand encapsulation, let's consider an example of a class that represents a bank. Suppose we have a class called ContaBancaria
that has a balance as an instance variable.
public class AccountBancaria {
private double balance;
public Bank Account(double initial balance) {
if (initialbalance > 0) {
this.saldo = initialbalance;
}
}
public void deposit(double value) {
if (value > 0) {
balance += value;
}
}
public boolean withdraw(double value) {
if (value > 0 && balance >= value) {
balance -= value;
return true;
}
return false;
}
public double getBalance() {
return balance;
}
}
In this example, the variable balance
is marked as private
, which means that it cannot be accessed directly from outside the ContaBancaria
class . Instead, public methods such as deposit()
, cash()
and getSaldo()
are provided to allow operations to be performed on the balance of controlled manner.
The deposit()
method allows you to add money to the balance, but only if the amount is positive. The withdraw()
method allows you to withdraw money, but also checks if the amount is positive and if there is sufficient balance. The getSaldo()
method is a "getter" that allows other classes to obtain the current balance, but without the possibility of modifying it directly.
In addition, the constructor of the ContaBancaria
class also imposes a rule that the initial balance must be greater than zero. This is part of encapsulation, as it ensures that the bank account starts in a valid state.
Encapsulation also allows the internal implementation of a class to be changed without affecting the classes that use it. For example, if we decide to change the way the balance is stored or calculated internally in the ContaBankaria
class, we would not need to modify the code of other classes that depend on it, as long as the public methods remain the same. p>
Another important aspect of encapsulation is that it allows the creation of "setters", methods that allow modifying the value of an instance variable in a controlled manner. For example, if we wanted to allow changing the balance, we could add a setSaldo()
method that would include validations before actually changing the value.
In summary, encapsulation is an essential practice in object-oriented programming. It protects an object's data, maintains object integrity, facilitates code maintenance and evolution, and promotes code reuse. When designing your classes in Java, always apply encapsulation to ensure a robust and reliable software design.
Finally, it's important to remember that encapsulation is not just about hiding data or making it private. It's about defining a clear and consistent interface for your classes, which makes your objects easier to understand and use correctly. Encapsulation helps prevent inappropriate use of a class and ensures that business rules are applied, making software more reliable and easier to maintain.