[!INDEX]

  1. Definition
  2. Code Example employee name age with validation
  3. Explanation
  4. Example Without Encapsulation:
  5. Without Encapsulation Explain
  6. Benefits of Encapsulation:
  7. Summary

    1. Definition

[!NOTE] Encapsulation refers to the practice of hiding the internal details (data and methods) of a class and providing controlled access to them through public methods. This promotes data protection, security, and modularity.

Key Concepts of Encapsulation:
  1. Data Hiding: The internal representation of an object (its state, usually held in fields or variables) is hidden from the outside world. Only the object's methods are exposed to interact with its data.

  2. Controlled Access: By using methods (getters and setters), you can control how the internal state of an object is accessed or modified. This allows for validation and control over what changes can be made to the object's state.

  3. Private Fields: The data (instance variables) is typically declared private, meaning it cannot be accessed directly from outside the class.

  4. Public Methods: The class provides public methods (getters and setters) to allow controlled access to the data if necessary.


2. Code Example employee name age with validation

class Employee {
    // Private fields (data is hidden)
    private String name;
    private int age;

    // Constructor to initialize the fields
    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter method for name
    public String getName() {
        return name;
    }

    // Setter method for name (controlled access)
    public void setName(String name) {
        this.name = name;
    }

    // Getter method for age
    public int getAge() {
        return age;
    }

    // Setter method for age (controlled access with validation)
    public void setAge(int age) {
        if (age > 0) { // Validation: age must be positive
            this.age = age;
        } else {
            System.out.println("Age must be positive.");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee("John", 25);

        // Accessing the private fields via public methods
        System.out.println("Employee Name: " + emp.getName());
        System.out.println("Employee Age: " + emp.getAge());

        // Modifying the private fields via setters
        emp.setName("Jane");
        emp.setAge(30);

        System.out.println("Updated Employee Name: " + emp.getName());
        System.out.println("Updated Employee Age: " + emp.getAge());

        // Trying to set invalid age
        emp.setAge(-5);  // Output: Age must be positive.
    }
}
Employee Name: John
Employee Age: 25
Updated Employee Name: Jane
Updated Employee Age: 30
Age must be positive.

3. Explanation:

[!NOTE]

  1. Private Fields: The variables name and age are private, so they cannot be accessed or modified directly from outside the class.

  2. Getter and Setter Methods: The getName(), setName(), getAge(), and setAge() methods provide controlled access to the private fields. The setAge() method includes validation to ensure that the age is not negative.

  3. Data Protection: The internal details (like validation logic) are hidden from the outside world. This allows the class to maintain control over how its data is accessed and modified.


4. Example Without Encapsulation:

class Employee {
    // Public fields (no encapsulation)
    public String name;
    public int age;
}

public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee();

        // Directly modifying internal state
        emp.name = "John";
        emp.age = 25;

        // Printing the employee's state
        System.out.println("Employee Name: " + emp.name);
        System.out.println("Employee Age: " + emp.age);

        // Uncontrolled modification of internal state
        emp.age = -5;  // Invalid age
        System.out.println("Updated Employee Age: " + emp.age);  // No validation
    }
}
Employee Name: John
Employee Age: 25
Updated Employee Age: -5

5. - Without Encapsulation Explain

[!NOTE]

  1. In the above code, the name and age fields of the Employee class are marked as public. This means they can be accessed and modified directly from outside the class.
  2. In the main method, we set the age of the employee to -5, which is clearly invalid but is accepted because there is no control or validation. This modification happens without any restriction, leading to incorrect data in the object's internal state.
  3. There is no validation logic, so even incorrect or meaningless values can be assigned

6. Benefits of Encapsulation:

[!NOTE]

  1. Data Security: Encapsulation prevents external objects from directly modifying the internal state of an object in an uncontrolled way.

  2. Modularity: By hiding the implementation details, the class becomes more modular. You can change the internal implementation without affecting the code that uses the class.

  3. Easier Maintenance: With encapsulation, it becomes easier to maintain and update code because changes to the class internals don't impact the classes that depend on it.

  4. Increased Flexibility: You can easily control how the data is accessed and modified. For example, you can add validation logic to setter methods.


7. Summary:

[!NOTE] Encapsulation in Java is the practice of bundling data (fields) and methods that operate on the data into a single unit (a class), while restricting direct access to some of the object's components (usually with private access). This leads to better data protection, modularity, and control over how the internal state of an object is managed.