Encapsulation is one of the core principles of Object-Oriented Programming (OOP) that focuses on bundling data (attributes) and methods (functions) that operate on that data into a single unit called a class. It also involves controlling the access to the data by using access modifiers. Encapsulation helps to hide the internal details of an object and provides a well-defined interface for interacting with the object's data.
Example of Encapsulation:
Let's consider a simple example of encapsulation using a Person
class:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String newName) {
if (newName != null && !newName.isEmpty()) {
name = newName;
}
}
public int getAge() {
return age;
}
public void setAge(int newAge) {
if (newAge >= 0) {
age = newAge;
}
}
}
In this Person
class:
The
name
andage
attributes are declared asprivate
. This makes them inaccessible directly from outside the class.Public methods like
getName()
andsetName(String newName)
provide controlled access to thename
attribute. Similarly, methods likegetAge()
andsetAge(int newAge)
provide controlled access to theage
attribute.The
setName
andsetAge
methods have conditions to ensure that only valid data is assigned.
Usage of Encapsulation:
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice", 30);
// Accessing data using methods
System.out.println("Name: " + person.getName()); // Output: Name: Alice
System.out.println("Age: " + person.getAge()); // Output: Age: 30
// Modifying data using methods
person.setName("Bob");
person.setAge(-5); // Will be ignored due to the condition
System.out.println("Updated Name: " + person.getName()); // Output: Updated Name: Bob
System.out.println("Updated Age: " + person.getAge()); // Output: Updated Age: 30
}
}
In this example:
The attributes
name
andage
are not accessible directly from outside the class due to theirprivate
access modifier.Access to these attributes is provided through getter and setter methods.
Setter methods have conditions that control the modification of data.
Encapsulation ensures data integrity and enables you to enforce rules and validation before modifying attributes. It also provides a clear interface for using the class, making it easier to understand and maintain code.