Introduction
Encapsulation in Java means combining data and the methods that work on it inside a single class while restricting direct access to that data. It is one of the main principles of object-oriented programming. Encapsulation helps protect an object’s data from unwanted or invalid changes.
For example, a bank account can prevent users from directly changing its balance and provide methods for depositing or withdrawing money.
Encapsulation
How Is Encapsulation Achieved?
Encapsulation is commonly achieved by:
Declaring class attributes as
privateProviding
publicmethods to access or update themValidating values before modifying the attributes
Getter methods read private data, while setter or other update methods modify it.
Getter and Setter Methods
1. Getter Method
A getter method returns the value of a private attribute.
public String getName() { return name;}2. Setter Method
A setter method updates the value of a private attribute. It can also validate the value before updating it.
public void setName(String name) { this.name = name;}The this keyword refers to the current object. In this.name = name, this.name represents the class attribute, while name represents the method parameter.
Features of Encapsulation
Protects an object’s internal data
Provides controlled access to attributes
Allows values to be validated
Keeps related data and methods together
Makes code easier to maintain
Encapsulation Example in Java
// Class demonstrating encapsulationclass Student { // Private attributes cannot be accessed directly outside the class private String name; private int age; // Getter method returns the student's name public String getName() { return name; } // Setter method updates the student's name public void setName(String name) { this.name = name; } // Getter method returns the student's age public int getAge() { return age; } // Setter method validates and updates the student's age public void setAge(int age) { if (age > 0) { this.age = age; } else { System.out.println("Age must be greater than zero."); } }}public class Main { public static void main(String[] args) { // Create an object of the Student class Student student = new Student(); // Set private attribute values using setter methods student.setName("Aman"); student.setAge(20); // Get private attribute values using getter methods System.out.println("Name: " + student.getName()); System.out.println("Age: " + student.getAge()); }}In this example, name and age cannot be accessed directly because they are private. The getter methods return their values, while the setter methods update them. The setAge() method also prevents an invalid age from being stored.
Encapsulation in Java
Advantages of Encapsulation
Data protection: Private attributes cannot be accessed directly.
Controlled updates: Methods control how values are changed.
Validation: Invalid values can be rejected.
Flexibility: Internal code can be changed without affecting other parts of the program.
Maintainability: Classes become easier to understand and manage.
Summary
Encapsulation in Java means combining data and the methods that operate on it within a class while restricting direct access to the data. It is commonly achieved by declaring attributes as private and providing public getter and setter methods for controlled access.
Encapsulation protects data, allows validation, and prevents invalid or unwanted modifications. It also improves code maintainability and flexibility by keeping the internal implementation of a class separate from how other parts of the program use it.
Be the first to add a comment.