SrcForge

Lesson 23

Encapsulation

Encapsulation wraps data and methods into one unit. Private variables with getters and setters control access.

Java Track

Save progress as you learn.

61 lessons

Lesson content

Encapsulation wraps data and methods into a single class. Private variables with getters and setters control access.

Definition List

  • Encapsulation: Binding data and methods while restricting direct access.
  • Data Hiding: Protecting class data by declaring variables private.
  • Getter Method: Retrieves the value of a private variable.
  • Setter Method: Updates the value of a private variable.
  • Private Variable: Accessible only within the declaring class.

How to Achieve Encapsulation

  1. Declare class variables as private.
  2. Provide getter methods to read data.
  3. Provide setter methods to modify data.

Example 01: Getter and Setter Methods

class ShapeRectangle {
    private int length, width;

    int getLength() {
        return length;
    }

    int getWidth() {
        return width;
    }

    void setLength(int l) {
        if (l > 0)
            length = l;
        else
            length = 0;
    }

    void setWidth(int w) {
        if (w > 0)
            width = w;
        else
            width = 0;
    }

    int area() {
        return length * width;
    }
}

public class get_set {
    public static void main(String args[]) {
        ShapeRectangle o = new ShapeRectangle();
        o.setLength(10);
        o.setWidth(20);
        System.out.println("Length : " + o.getLength());
        System.out.println("Width  : " + o.getWidth());
        System.out.println("Area of Rectangle : " + o.area());
    }
}

Output

Length : 10
Width  : 20
Area of Rectangle : 200

Benefits of Encapsulation

  • Read-only: provide only getter methods.
  • Write-only: provide only setter methods.
  • Setters validate data before storing.
  • Sensitive data stays private.
  • Internal changes don't break external code.

Best Practices

  • Always declare variables as private.
  • Use getters to read values.
  • Use setters to update values.
  • Validate input inside setters.
  • Never expose sensitive data directly.

FAQ

1. What is encapsulation?

Hiding data and providing access through getters and setters.

2. Why declare variables private?

Prevents direct access and unauthorized modification.

3. What are getter and setter methods?

Getters read private values. Setters update them safely.

Encapsulation wraps data and methods together.
Declare variables private to hide them.
Getters read. Setters write.
Setters validate before storing values.
Protects sensitive data from outside access.
Changes inside class don't break other code.
Core pillar of Java OOP.

Where Java is used

Java in real-world development

Private Variables

Declared with private keyword. Only accessible within the same class.

Getter Methods

Return private variable values. Allow read-only access from outside.

Setter Methods

Update private variables. Can validate input before storing.

Data Hiding

Private variables hide internal data. Access only through methods.

Read-Only / Write-Only

Provide only getter for read-only. Only setter for write-only access.

Real-World Example

Bank balance is private. Deposit and withdraw methods control all access.