DEV Community

Cover image for πŸš€ Day 14 of My Automation JourneyπŸ” Encapsulation & Access Modifiers in Java
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 14 of My Automation JourneyπŸ” Encapsulation & Access Modifiers in Java

Today I learned one of the most important Object-Oriented Programming (OOP) concepts in Java β€” Encapsulation.

Encapsulation is used to protect data and control access to variables and methods inside a class.

πŸ“¦ What is Encapsulation?

The word Encapsulation comes from the concept of a Capsule.

A capsule keeps medicines safely enclosed inside it.

Similarly in Java:

Encapsulation means wrapping data (variables) and code (methods) together in a single unit (class) and restricting direct access to some components.

πŸ“Œ In simple words:

Encapsulation = Data Protection

🎯 Why Do We Use Encapsulation?

Encapsulation provides several advantages:

βœ” Protects sensitive data
βœ” Prevents unauthorized access
βœ” Improves code maintainability
βœ” Makes code more secure
βœ” Allows controlled access using methods

πŸ”‘ How Do We Protect Data in Java?

Java uses Access Modifiers to control access.

These modifiers define who can access variables, methods, and classes.

πŸ” Types of Access Modifiers in Java

Java provides four types of access modifiers:

| Access Modifier | Same Class | Same Package | Subclass | Different Package |
| --------------- | ---------- | ------------ | -------- | ----------------- |
|   private       |    βœ…      |     ❌      |    ❌    |       ❌         |
|   default       |    βœ…      |     βœ…      |    ❌    |       ❌         |
|   protected     |    βœ…      |     βœ…      |    βœ…    |       ❌*        |
|   public        |    βœ…      |     βœ…      |    βœ…    |       βœ…         |

Enter fullscreen mode Exit fullscreen mode

(*Protected works in another package only through inheritance)

πŸ’» Example Used in My Code

I created a class called Bala with variables using different access modifiers.

package developer;

public class Bala {

    private int salary = 10000;
    String work = "Development";
    protected String headphone = "sony";
    public String Designation = "Developer";

    public static void main(String[] args) {
        Bala bala1 = new Bala();
        System.out.println(bala1.salary);
    }
}
Enter fullscreen mode Exit fullscreen mode
| Variable    | Access Modifier | Access Level               |
| ----------- | --------------- | -------------------------- |
| salary      | private         | Only inside the same class |
| work        | default         | Same package only          |
| headphone   | protected       | Same package + subclass    |
| Designation | public          | Anywhere                   |
Enter fullscreen mode Exit fullscreen mode

Example:

package developer;

public class Nantha extends Bala {

    public static void main(String[] args) {

        Nantha NanthaKumar = new Nantha();

        System.out.println(NanthaKumar.work);
        System.out.println(NanthaKumar.headphone);
        System.out.println(NanthaKumar.Designation);

    }
}
Enter fullscreen mode Exit fullscreen mode

Accessible

βœ” default variable (work)
βœ” protected variable (headphone)
βœ” public variable (Designation)

Not Accessible

❌ private variable (salary)

🌍 Access From Different Package

Example:

package tester;

import developer.Bala;

public class Deepika extends Bala {

    public static void main(String[] args) {

        Deepika deepika = new Deepika();

        System.out.println(deepika.headphone);
        System.out.println(deepika.Designation);

    }
}
Enter fullscreen mode Exit fullscreen mode

Accessible

βœ” protected (through inheritance)
βœ” public

Not Accessible

❌ private
❌ default

πŸ§‘β€πŸ’» Access Using Object in Another Package

package tester;

import developer.Bala;

public class Kumar extends Bala {

    public static void main(String[] args) {

        Kumar kumar = new Kumar();
        System.out.println(kumar.Designation);

        Bala bala = new Bala();
        System.out.println(bala.Designation);

    }
}
Enter fullscreen mode Exit fullscreen mode

Here:

βœ” public variable is accessible everywhere.

⚠️ Exceptional Scenarios in Encapsulation

These are important situations where access modifiers behave differently.

1️⃣ Private Variable Access

Private variables cannot be accessed outside the class.

Example (Error):

NanthaKumar.salary
Enter fullscreen mode Exit fullscreen mode

βœ” Solution: Use Getter and Setter methods.

2️⃣ Default Modifier Limitation

Default variables cannot be accessed outside the package.

Example:

System.out.println(deepika.work);
Enter fullscreen mode Exit fullscreen mode

❌ Compilation error.

3️⃣ Protected Access in Different Package

Protected variables are accessible only through inheritance.

Example:

Deepika extends Bala
Enter fullscreen mode Exit fullscreen mode

βœ” Accessible.

But:

Bala b = new Bala();
b.headphone
Enter fullscreen mode Exit fullscreen mode

❌ Not accessible.

4️⃣ Public Access

Public members can be accessed from anywhere in the program.

Example:

System.out.println(bala.Designation);

βœ” Works everywhere.

5️⃣ Private Access Through Getter & Setter

In real-world projects, private variables are accessed through getter and setter methods.

Example:

private int salary;

public int getSalary(){
    return salary;
}
Enter fullscreen mode Exit fullscreen mode

This is the real implementation of encapsulation.

🧠 Key Takeaways

βœ” Encapsulation protects data
βœ” Access modifiers control visibility
βœ” Private variables improve security
βœ” Getter & Setter methods provide controlled access
βœ” Encapsulation is widely used in automation frameworks

πŸ‘¨β€πŸ« Trainer: Nantha from Payilagam

πŸ€– A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainer’s explanations.

Top comments (0)