What is Encapsulation?
Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP) in Java. It refers to the concept of wrapping data (variables) and methods (functions) into a single unit or class and restricting direct access to some of the object’s components. This helps to protect the internal state of the object and ensures that it can only be modified in controlled ways.
Key Aspects of Encapsulation: #
Private Variables: Data members (variables) of a class are made private to prevent direct access from outside the class. This ensures that the internal state of the object is not exposed or modified directly.
Public Methods: Access to the private data is provided through public methods known as getters and setters. These methods allow controlled access to the internal state of the object.
Data Hiding: By making variables private and providing public methods for access, encapsulation hides the internal implementation details from the outside world. This helps in maintaining control over the data and how it is modified.
Increased Flexibility and Maintainability: Encapsulation allows the implementation of a class to be changed without affecting the classes that use it. The internal workings of the class can be modified as long as the public interface (methods) remains consistent.
Example of Encapsulation: #
class Person {
// Private variables
private String name;
private int age;
// Public constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Public getter for name
public String getName() {
return name;
}
// Public setter for name
public void setName(String name) {
this.name = name;
}
// Public getter for age
public int getAge() {
return age;
}
// Public setter for age
public void setAge(int age) {
if (age > 0) { // Ensuring age is positive
this.age = age;
}
}
// Method to display person details
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
// Creating a Person object
Person person = new Person("Alice", 30);
// Accessing private variables via public methods
person.displayInfo();
// Modifying data via setter
person.setName("Bob");
person.setAge(35);
// Display updated information
person.displayInfo();
// Direct access to private variables is not allowed
// person.name = "Charlie"; // Error: name has private access in Person
// person.age = -5; // Error: age has private access in Person
}
}
Explanation of the Example: #
Private Variables:
name and age are private, so they cannot be accessed directly from outside the Person class.
Public Methods:
getName() and setName(String name) provide controlled access to the name variable.
getAge() and setAge(int age) provide controlled access to the age variable, including validation to ensure that the age is positive.
Data Hiding:
The internal representation of Person is hidden from the user, who can only interact with the class through its public methods.
Controlled Access:
The setters include validation (e.g., setAge(int age) ensures the age is positive) to protect the integrity of the data.
Summary #
Encapsulation is a key OOP principle that combines data and methods into a single class and restricts direct access to some of the object’s components. By using private variables and providing public methods for access, encapsulation enhances data security, provides flexibility in code maintenance, and ensures that the internal state of objects is controlled and managed effectively.