Understanding Access Modifiers in Java

Introduction

In this article, we will learn about Access Modifiers in Java. Access modifiers in Java are keywords used to control the visibility and accessibility of classes, variables, methods, and constructors. They determine which parts of your code can be accessed from other classes or packages.

Java provides four access modifiers, public, private, protected, and default (no keyword).

  1. Public Access Modifier: The public access modifier is the most permissive and allows access from anywhere in the program. A public class, variable, method, or constructor can be accessed from any other class, even in different packages.
  2. Private Access Modifier: The private access modifier is the most restrictive and allows access only within the same class. A private variable, method, or constructor cannot be accessed from any other class, even from subclasses in the same package.
  3. Protected Access Modifier: The protected access modifier allows access within the same package and from subclasses in different packages. A protected variable, method, or constructor can be accessed from within the same package and from subclasses in different packages.
  4. Default (no keyword) Access Modifier: If no access modifier is specified, it is considered the default access modifier. Classes with the default access modifier are only accessible within the same package. Variables, methods, and constructors with the default access modifier can be accessed from any class within the same package.

Let us take an Example to demonstrate access modifiers in Java.

public class Student {
    // public variable
    public String name;

    // private variable
    private int age;

    // protected variable
    protected double gpa;

    // default variable
    String major;

    // public constructor
    public Student(String name, int age, double gpa, String major) {
        this.name = name;
        this.age = age;
        this.gpa = gpa;
        this.major = major;
    }

    // public method
    public void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("GPA: " + gpa);
        System.out.println("Major: " + major);
    }

    // private method
    private boolean isEligibleForScholarship() {
        return gpa >= 3.5;
    }

    // protected method
    protected void applyForScholarship() {
        if (isEligibleForScholarship()) {
            System.out.println(name + " is eligible for a scholarship.");
        } else {
            System.out.println(name + " is not eligible for a scholarship.");
        }
    }
}

// Class in the same package
class UniversityCourse {
    public static void main(String[] args) {
        Student student = new Student("Aditya Singh", 20, 3.8, "Computer Science");

        // Accessing public members
        student.name = "Lokendra Singh";
        student.displayInfo();

        // Cannot access private members
        // student.age = 21; // Compilation error

        // Accessing protected members through an instance
        student.gpa = 3.9;
        student.applyForScholarship();

        // Accessing default members
        student.major = "Data Engineering";
    }
}

In the above example

  • the name is a public variable, so it can be accessed from anywhere, including modifying its value directly.
  • age is a private variable, so it cannot be accessed directly from outside the Student class.
  • GPA is a protected variable, so it can be accessed from within the same package (UniversityCourse) and from subclasses in different packages.
  • major is a default variable, so it can be accessed from within the same package (UniversityCourse).
  • The displayInfo() method is public, so it can be called from anywhere.
  • The isEligibleForScholarship() method is private, so it can only be called from within the Student class.
  • The applyForScholarship() method is protected, so it can be called from within the same package (UniversityCourse) and from subclasses in different packages.

Summary

It's important to choose the appropriate access modifier based on your requirements for encapsulation, code organization, and maintainability. In general, it's a good practice to use the most restrictive access modifier that meets your needs, following the principle of least privilege. By understanding and properly using access modifiers, you can control the visibility and accessibility of your Java classes, variables, methods, and constructors, ensuring better code organization, encapsulation, and maintainability.


Similar Articles