Encapsulation In Java

Java is a powerful object-oriented programming language that follows the principles of encapsulation to help developers maintain their code and protect the integrity of their data. Encapsulation in Java can be implemented in several ways, such as Through Access Modifiers, Data Hiding, Class Encapsulation and Proxies.

Access Modifiers

Access modifiers are used for controlling access to members or classes. By default, all classes and members in Java are public. This means that anyone can access them. We can use the access modifiers public , protected, default and private, to control access.

Incess modifiers can also be used in concert with inheritance to control inheritance. For example, if a class is declared as public, then it is accessible to all other classes, but if a class is declared as private, then it cannot be inherited.

public class Employee { private String name; public int employeeID; protected void setName(String name) { this.name = name; } public String getName() { return this.name; } }

In the above example, the field name has been declared as private, which means that it is not accessible to any class, but the method setName() which is declared as protected means that only classes in the same package or subclasses can access it.

Data Hiding

Data hiding is another way of implementing encapsulation in Java. This method prevents the user from having access to the internal data of a class, and instead provides access only through the public method of the class.

For example, the following code defines a Car class which has start() and stop() methods, but the internal data of the car is only visible within the Car class.

public class Car { private boolean engineOn; public void start(){ engineOn = true; } public void stop(){ engineOn = false; } }
Class Encapsulation

Class encapsulation is another way of controlling access to data. By encapsulating a class we can limit access to certain parts of the class. This is done by providing a set of methods (getters and setters) that allow access to the data, while hiding the data from direct access by other classes.

For example, in the Car class defined above, we can encapsulate the engineOn field by providing a separate isEngineOn() method that returns the boolean value of the engine state.

public class Car { private boolean engineOn; public void start(){ engineOn = true; } public void stop(){ engineOn = false; } public boolean isEngineOn(){ return engineOn; } }
Proxies

Proxies are another type of data hiding mechanism that is commonly used in Java. In this approach, the class that is actually using the data to perform operations is completely separate from the class that actually holds the data. It works by wrapping access to the data with a proxy. This is useful when you want to hide the implementation details of the data, while still allowing access to the data.

For example, a proxy could be used to wrap the Car class defined above. The proxy can have methods like accelerate() and brake() that perform operations on the engineOn field without having direct access to it.

public class CarProxy { private Car car; public CarProxy(Car car) { this.car = car; } public void accelerate() { car.start(); } public void brake() { car.stop(); } }

Encapsulation in Java can be implemented in various ways, from access modifiers to data hiding to class encapsulation to proxies. Depending on the requirements, any of these approaches can be used to help developers maintain their code and protect the integrity of their data.