how we can achieve encapsulation in java
first we have to know that What is encapsulation in java then we will go for how we can achieve encapsulation in java
Encapsulation in Java is one of the four fundamental Object-Oriented Programming (OOP) concepts, alongside inheritance, polymorphism, and abstraction. It refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit, known as a class. The data is kept private, and access to it is controlled through public methods, which are also known as getter and setter methods.
![how we can achieve encapsulation in java how we can achieve encapsulation in java](https://www.testpreparationz.com/wp-content/uploads/2024/01/Encapsulation-in-java-1024x560.png)
Achieving encapsulation in Java involves the following key practices:
1. Declare Data Members as Private: Designate the data members (fields or attributes) of a class as private. This ensures that they can only be accessed within the class.
public class MyClass {
private int myNumber;
private String myString;
}
2. Provide Public Getter and Setter Methods: Create public methods (getters and setters) to access and modify the private data. These methods allow controlled access to the encapsulated data.
public class MyClass {
private int myNumber;
private String myString;
public int getMyNumber() {
return myNumber;
}
public void setMyNumber(int myNumber) {
// Validation or additional logic can be added here
this.myNumber = myNumber;
}
public String getMyString() {
return myString;
}
public void setMyString(String myString) {
// Validation or additional logic can be added here
this.myString = myString;
}
}
3. Use Constructor Initialization: Initialize the private data members through constructors. This helps in ensuring that the object is in a valid state upon creation.
public class MyClass {
private int myNumber;
private String myString;
// Constructor
public MyClass(int myNumber, String myString) {
this.myNumber = myNumber;
this.myString = myString;
}
}
4. Apply Validation and Business Logic: Within the setter methods, incorporate validation and business logic to ensure that the data remains in a consistent and valid state.
public class MyClass {
private int myNumber;
private String myString;
public void setMyNumber(int myNumber) {
if (myNumber > 0) {
this.myNumber = myNumber;
} else {
System.out.println("Invalid value for myNumber. Must be greater than 0.");
}
}
public void setMyString(String myString) {
if (myString != null && !myString.isEmpty()) {
this.myString = myString;
} else {
System.out.println("Invalid value for myString. Cannot be null or empty.");
}
}
}
By following these practices, you encapsulate the internal details of a class and control access to its data. This promotes data integrity, enhances code maintainability, and allows for the implementation of necessary logic within the class itself. Encapsulation is a fundamental principle in object-oriented programming that contributes to the creation of modular and well-organized code.