DEV Community

Karthick Narayanan
Karthick Narayanan

Posted on

Day 15: Encapsulation and Access Modifiers in Java

What is Encapsulation?

Encapsulation means data protection.

In Java, encapsulation is the concept of wrapping data (variables) and methods together and controlling who can access them.

πŸ‘‰ In simple words:
Encapsulation = Protecting data from unauthorized access


How is Encapsulation Achieved in Java?

Encapsulation is achieved using Access Modifiers.

Access modifiers decide:

  • Who can access a class
  • Who can access variables
  • Who can access methods
  • Who can access constructors

What are Access Modifiers?

Access modifiers define the visibility of class members.

Java has 4 access modifiers:

  1. private
  2. default (no keyword)
  3. protected
  4. public

Example

Parent Class: Nanthakumar

public class Nanthakumar {

    private int salary = 10000;

    String task = "Dev";

    protected String headPhone = "redmi";

    public String designation = "Development";
}
Enter fullscreen mode Exit fullscreen mode

Here we have declared variables with different access modifiers.


Main Class: Karthick

public class Karthick {

    public static void main(String[] args) {

        Karthick k = new Karthick();
        // System.out.println(k.salary); // Not allowed

        Nanthakumar n = new Nanthakumar();
        System.out.println(n.task);
        System.out.println(n.headPhone);
        System.out.println(n.designation);
    }
}
Enter fullscreen mode Exit fullscreen mode

Child Class: Ruby

public class Ruby extends Nanthakumar {

    public static void main(String[] args) {

        Nanthakumar n = new Nanthakumar();
        // System.out.println(n.task); // Not allowed

        Ruby r = new Ruby();
        System.out.println(r.headPhone);
        System.out.println(r.designation);
    }
}
Enter fullscreen mode Exit fullscreen mode

Understanding Each Access Modifier

1️⃣ Private

private int salary = 10000;
Enter fullscreen mode Exit fullscreen mode
  • Accessible only inside the same class
  • Cannot be accessed outside the class
  • Used for maximum data protection

❌ Not accessible in Karthick
❌ Not accessible in Ruby

πŸ‘‰ This shows strong encapsulation


2️⃣ Default (No Keyword)

String task = "Dev";
Enter fullscreen mode Exit fullscreen mode
  • Accessible within the same package
  • Not accessible outside the package
  • Also called package-private

βœ” Accessible in Karthick (same package)
❌ Not accessible in Ruby (different class via inheritance)


3️⃣ Protected

protected String headPhone = "redmi";
Enter fullscreen mode Exit fullscreen mode
  • Accessible within the same package
  • Accessible in child class even outside the package

βœ” Accessible in Karthick
βœ” Accessible in Ruby (because it extends Nanthakumar)


4️⃣ Public

public String designation = "Development";
Enter fullscreen mode Exit fullscreen mode
  • Accessible from anywhere
  • No restriction

βœ” Accessible in Karthick
βœ” Accessible in Ruby


Access Modifier Visibility Table

Modifier Same Class Same Package Subclass Anywhere
private βœ” ❌ ❌ ❌
default βœ” βœ” ❌ ❌
protected βœ” βœ” βœ” ❌
public βœ” βœ” βœ” βœ”

Why Encapsulation is Important?

  • Protects sensitive data
  • Prevents unauthorized access
  • Improves code security
  • Makes code easy to maintain
  • Follows real-world object behavior

Real-Life Example

Think of an ATM card:

  • PIN β†’ private
  • Bank branch access β†’ protected
  • Customer details β†’ default
  • Bank name β†’ public

You cannot access everything directly β€” this is encapsulation.


Key Points (For Revision)

  • Encapsulation means data protection
  • Achieved using access modifiers
  • private β†’ most secure
  • public β†’ accessible everywhere
  • protected β†’ inheritance-friendly
  • default β†’ package-level access

Top comments (0)