DEV Community

Athithya Sivasankarar
Athithya Sivasankarar

Posted on

Understanding Java Access Modifiers

When learning Java, one of the first Object-Oriented Programming (OOP) concepts you'll encounter is Access Modifiers. They control the visibility and accessibility of classes, variables, methods, and constructors.
Java provides four access modifiers:
public
private
protected
default (no access modifier)
In this blog, we'll explore each access modifier with simple examples to help beginners understand them easily.
What are Access Modifiers?
Access Modifiers are Java keywords that define where a class, method, variable, or constructor can be accessed.
They help provide security, encapsulation, and controlled access to data.
Why Do We Need Access Modifiers?
Access modifiers help us:
Protect sensitive data.
Prevent unauthorized access.
Improve code security.
Support Object-Oriented Programming principles.
Control how different classes interact with each other.

  1. Public Access Modifier
    The public access modifier allows a class member to be accessed from anywhere in the program.
    Example
    class Student {

    public String name = "Athithya";

    public void display() {
    System.out.println("Student Name: " + name);
    }
    }

public class Main {

public static void main(String[] args) {

    Student s = new Student();

    s.display();

}
Enter fullscreen mode Exit fullscreen mode

}
Output
Student Name: Athithya
Explanation
Since the variable and method are declared as public, they can be accessed from any class.

  1. Private Access Modifier
    The private access modifier allows access only within the same class.
    Example
    class BankAccount {

    private int balance = 5000;

    public void showBalance() {
    System.out.println(balance);
    }

}

public class Main {

public static void main(String[] args) {

    BankAccount account = new BankAccount();

    account.showBalance();

}
Enter fullscreen mode Exit fullscreen mode

}
Output
5000
Explanation
The variable balance cannot be accessed directly outside the class.
The following code will produce an error:
System.out.println(account.balance);
This improves data security.

  1. Protected Access Modifier
    The protected access modifier allows access:
    Within the same class
    Within the same package
    In subclasses (inheritance), even if they are in different packages
    Example
    class Animal {

    protected void sound() {
    System.out.println("Animal makes a sound");
    }

}

class Dog extends Animal {

void bark() {
    sound();
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {

public static void main(String[] args) {

    Dog d = new Dog();

    d.bark();

}
Enter fullscreen mode Exit fullscreen mode

}
Output
Animal makes a sound
Explanation
Since Dog inherits Animal, it can access the protected method.

  1. Default Access Modifier
    If no access modifier is specified, Java automatically assigns the default access modifier.
    It is accessible only within the same package.
    Example
    class Employee {

    String department = "Cyber Security";

    void display() {

    System.out.println(department);
    

    }

}

public class Main {

public static void main(String[] args) {

    Employee e = new Employee();

    e.display();

}
Enter fullscreen mode Exit fullscreen mode

}
Output
Cyber Security
Explanation
Since no access modifier is written, both the variable and method have default access.
They can be accessed only within the same package.

Top comments (0)