🔹 Access Modifiers in Object-Oriented Programming (OOP)

Access modifiers are **keywords** in OOP that control the **visibility and accessibility** of **classes, methods, and variables**. They help enforce **data security, encapsulation, and proper code structure**.

📌 Why Are Access Modifiers Important?

Access modifiers define **who can access a class or its members** (variables and methods). They help:

  • ✅ **Protect sensitive data** from unauthorized access.
  • ✅ **Ensure encapsulation** by limiting access to class internals.
  • ✅ **Prevent accidental modifications** of critical variables.
  • ✅ **Improve code maintainability** by enforcing clear access rules.

📌 Types of Access Modifiers

🔹 1. Private (Most Restricted)

The **private** modifier means that the variable or method **can only be accessed within the same class**. It is not visible to subclasses or other classes.

✅ **Use Case:** Hiding sensitive data, like passwords or bank balances.


            class BankAccount {
                private double balance; // Private: Only accessible within this class
                
                public void deposit(double amount) {
                    balance += amount;
                }
                
                public double getBalance() { // Public method to access private data
                    return balance;
                }
            }
            
            public class Main {
                public static void main(String[] args) {
                    BankAccount account = new BankAccount();
                    account.deposit(500);
                    System.out.println(account.getBalance()); // ✅ Allowed
                    // System.out.println(account.balance); ❌ ERROR (Cannot access private variable)
                }
            }
                

🔹 2. Default (Package-Private)

If no access modifier is specified, the default modifier is applied. A **default member is accessible only within the same package**.

✅ **Use Case:** When different classes within the same package need access to shared functionality.


            // Default modifier (No 'public', 'private', or 'protected' specified)
            class PackageExample {
                void showMessage() {
                    System.out.println("Accessible only within the same package");
                }
            }
                

🔹 3. Protected (Accessible to Subclasses)

The **protected** modifier allows access within the **same package and by subclasses (even in different packages).**

✅ **Use Case:** When a subclass should inherit but not modify a method.


            class Animal {
                protected void makeSound() {
                    System.out.println("Animal makes a sound");
                }
            }
            
            class Dog extends Animal {
                public void bark() {
                    makeSound(); // ✅ Allowed because of protected access
                    System.out.println("Dog barks");
                }
            }
                

🔹 4. Public (No Restrictions)

The **public** modifier allows unrestricted access from anywhere in the program.

✅ **Use Case:** When a method should be accessible to all classes.


            class Car {
                public void start() {
                    System.out.println("Car Started");
                }
            }
            
            public class Main {
                public static void main(String[] args) {
                    Car myCar = new Car();
                    myCar.start(); // ✅ Allowed from anywhere
                }
            }
                

📝 Summary of Access Modifiers

Access Modifier Within Class Within Package Subclasses Outside Package
Private ✅ Yes ❌ No ❌ No ❌ No
Default ✅ Yes ✅ Yes ❌ No ❌ No
Protected ✅ Yes ✅ Yes ✅ Yes ❌ No
Public ✅ Yes ✅ Yes ✅ Yes ✅ Yes

🔗 Next Topics