Java Access Modifiers


In Java, access modifiers are keywords used to define the visibility and accessibility of classes, methods, constructors, and variables. They are used to specify the level of access control for these members, ensuring that data is protected and accessible only where needed.

There are four primary types of access modifiers in Java:

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

In this comprehensive guide, we'll explore each of these access modifiers in detail, including their usage and best practices.

Table of Contents

  1. What Are Java Access Modifiers?
  2. Types of Access Modifiers
  3. Access Modifiers in Java Classes
  4. Access Modifiers in Java Methods
  5. Access Modifiers in Java Variables

What Are Java Access Modifiers?

In Java, access modifiers are used to define the scope and visibility of classes, methods, and other members. They determine how other parts of a program can access these members, whether they can be accessed from other classes or packages, or whether they are restricted to certain areas of the code.

Access modifiers play a crucial role in encapsulation, one of the fundamental principles of object-oriented programming. By controlling access to class members, they help maintain a clear and manageable interface for your code and protect its internal implementation.


Types of Access Modifiers

Public Access Modifier

The public access modifier allows the widest level of access. A member declared as public can be accessed from any other class or package in your program. This means that there are no restrictions on access to that member.

Example: Public Access Modifier

public class Car {
    public String model;
    
    public void display() {
        System.out.println("Model: " + model);
    }
}

In the example above, the model variable and the display() method are both public, meaning they can be accessed from anywhere, even from classes in other packages.

// In another class
Car car = new Car();
car.model = "Toyota";  // Accessing the public field
car.display();         // Calling the public method

Key Points:

  • Members marked as public are accessible from any other class, regardless of the package.
  • Typically used when you want the class or method to be widely accessible.

Private Access Modifier

The private access modifier is the most restrictive access level. A member declared as private can only be accessed within the same class in which it is declared. This is the core principle of encapsulation — keeping the internal workings of a class hidden from outside interference.

Example: Private Access Modifier

public class Car {
    private String model;
    
    public void setModel(String model) {
        this.model = model;
    }
    
    public void display() {
        System.out.println("Model: " + model);
    }
}

In this example, the model variable is private, so it cannot be accessed directly from outside the Car class. The only way to modify or retrieve the model is through the setModel() method.

// In another class
Car car = new Car();
car.setModel("Toyota");  // Setting the model using the public method
car.display();           // Displaying the model

Key Points:

  • Members marked as private are only accessible within the same class.
  • Used to hide implementation details and to protect data from unwanted access or modification.

Protected Access Modifier

The protected access modifier allows access within the same package (like the default modifier) and also to subclasses (even if they are in different packages). This provides a balance between encapsulation and extending functionality in subclasses.

Example: Protected Access Modifier

public class Car {
    protected String model;
    
    protected void display() {
        System.out.println("Model: " + model);
    }
}

In this example, the model variable and the display() method are both protected, meaning they can be accessed within the Car class, any class in the same package, or by subclasses, even if the subclass is in a different package.

// In a subclass (even if in a different package)
public class ElectricCar extends Car {
    public void showModel() {
        System.out.println("Electric Car Model: " + model);
    }
}

Key Points:

  • Members marked as protected are accessible within the same package and to subclasses (even if they are in different packages).
  • Useful for creating class hierarchies, allowing subclasses to access inherited members.

Default (Package-Private) Access Modifier

If no access modifier is specified, the default access level is package-private (also known as the default access modifier). This means that the member is accessible only within the same package. It cannot be accessed from classes in other packages.

Example: Default Access Modifier

public class Car {
    String model;  // Default access modifier (package-private)
    
    void display() {
        System.out.println("Model: " + model);
    }
}

In this example, both the model variable and the display() method have the default access modifier, which means they are only accessible within classes in the same package.

// In another class in the same package
Car car = new Car();
car.model = "Toyota";  // Accessing the default variable
car.display();         // Calling the default method

Key Points:

  • Members with no access modifier are accessible only within the same package.
  • This is the default level of access and is often used for package-level encapsulation.

Access Modifiers in Java Classes

Java classes can also have access modifiers. By default, classes can only have public or default access modifiers. If you do not specify an access modifier, the class will have package-private access.

  • public: A class marked as public can be accessed from any other class, even if they are in different packages.
  • default: A class without an access modifier is only accessible within the same package.

Example: Access Modifiers in Classes

public class PublicClass {  // Can be accessed from any package
    // Class content
}

class DefaultClass {  // Package-private: Can only be accessed within the same package
    // Class content
}

Access Modifiers in Java Methods

Access modifiers can also be applied to methods. You can use public, private, protected, or the default access modifier for methods to define who can access and invoke those methods.

Example: Access Modifiers in Methods

public class Car {
    private void privateMethod() {
        System.out.println("This is a private method");
    }

    public void publicMethod() {
        System.out.println("This is a public method");
    }
    
    protected void protectedMethod() {
        System.out.println("This is a protected method");
    }
}

Access Modifiers in Java Variables

Access modifiers can be used to define the visibility of variables as well. You can control who can read and modify the variables, making them an essential part of encapsulating data.

Example: Access Modifiers in Variables

public class Car {
    private String model;  // Private variable
    
    public void setModel(String model) {
        this.model = model;  // Setting the value of private variable
    }
    
    public String getModel() {
        return model;  // Accessing the private variable through a public method
    }
}