Access Modifiers in Java: Public, Protected, Default, and Private Explained

Access Modifiers in Java

An access modifier in Java is a keyword used to control the visibility and accessibility of class members such as variables, methods, constructors, and classes.
Access modifiers help achieve data security, encapsulation, and controlled access.

✅ Types of Access Modifiers in Java

Java provides four types of access modifiers:
  1. public
  2. protected
  3. default (no keyword)
  4. private

🌍 Real-Life Analogy

Think of a house:
  • Public areas → Living room (everyone can access)
  • Protected areas → Family room (relatives only)
  • Default areas → Inside the house (family members)
  • Private areas → Bedroom (only owner)

1. Public Access Modifier
✅ Definition

If a class member is declared as public, it can be accessed:
  • Within the same class
  • Within the same package
  • From a different package
✔ No restriction on access.
public int data;
public void show() {}

🌍 Real-Life Example

Public Park – anyone can enter and use it.

2. Protected Access Modifier
✅ Definition

If a class member is declared as protected, it can be accessed:
  • Within the same class
  • Within the same package
  • In a different package only through inheritance
            protected int value;
            protected void display() {}

🌍 Real-Life Example

Family Property – accessible only to family members (inheritance).

3. Default Access Modifier (Package-Private)
✅ Definition

When no access modifier is specified, it is called default access.
It can be accessed:
  • Within the same class
  • Within the same package
  • ❌ Not accessible outside the package

            int number;
            void print() {}

🌍 Real-Life Example

Apartment Society – only residents can enter.

4. Private Access Modifier

✅ Definition

If a class member is declared as private, it can be accessed:
  • Only within the same class

                     private int secret;

                     private void calculate() {}

🌍 Real-Life Example

ATM PIN – known only to the account holder.

🔑 Where Access Modifiers Can Be Used

✅ Public

  • Class
  • Abstract Class
  • Interface
  • Static Variable
  • Instance Variable
  • Static Method
  • Instance Method
  • Constructor

✅ Protected

  • Static Variable
  • Instance Variable
  • Static Method
  • Instance Method
  • Constructor

✅ Default

  • Class
  • Abstract Class
  • Interface
  • Static Method
  • Instance Method
  • Constructor
  • Static Block
  • Instance Block
  • Local Variable

✅ Private

  • Static Variable
  • Instance Variable
  • Static Method
  • Instance Method
  • Constructor
❌ Private classes and interfaces are not allowed

⚠️ Rules of Access Modifiers in Method Overriding
✅ Important Rule

A subclass overriding method cannot reduce visibility, but it can increase visibility.

❌ Invalid Example

class A {
    public void show() {}
}

class B extends A {
    protected void show() {} // ❌ Not allowed
}

✅ Valid Example

class A {
    protected void show() {}
}

class B extends A {
    public void show() {} // ✅ Allowed
}

📌 Key Points to Remember

  • Access modifiers control visibility
  • public has the highest visibility
  • private has the lowest visibility
  • Proper use improves security and encapsulation
  • Method overriding must not reduce access level

Comments

Popular posts from this blog

History of Java Programming Language | Features, Origin & Uses

Inheritance in Java: Types, Examples, and Explanation

Java Programming Features Every Beginner Should Know