Access Specifiers in Java

Access Specifiers in Java

Access specifiers in Java(also called access modifiers) are keywords utilized to indicate the visibility (availability) of classes, methods, and variables.  They control the level of access to members of a class from other classes or packages.

Access Specifiers in Java
Access Specifiers in Java

There are four main types of access specifiers in Java:

  1. public
  2. private
  3. protected
  4. Default (Package-Private) – No specifier used

Each access specifier in java defines how the members (fields, methods, classes, etc.) can be accessed.

1. public Access Specifier

The public access specifier is the least restrictive. A member declared as public can be accessed from anywhere in the program.

  • Class: A class declared as public can be gotten to from any other class.
  • Method/Field: A method or field declared as public can be gotten to from anyplace.

Example of public access specifier:

public class Car {
public String brand;
public int year;

// Public method
public void displayDetails() {
System.out.println(“Brand: ” + brand + “, Year: ” + year);
}
}

public class Main {
public static void main(String[] args) {
Car car = new Car();
car.brand = “Toyota”; // Accessible because it’s public
car.year = 2024; // Accessible because it’s public
car.displayDetails(); // Accessible because it’s public
}
}

In this example, the Car class, the brand field, and the displayDetails method are all public, so they can be accessed anywhere, even from other classes.

2. private Access Specifier

The private access specifier is the foremost prohibitive. A member declared as private can as it were be accessed inside the same class. It cannot be accessed outside the class, not indeed by subclasses or classes within the same package.

  • Method/Field: A private method or field can only be accessed inside the class where it is defined.

Example of private access specifier:

class Car {
private String brand; // private field
private int year; // private field

// Private method
private void displayDetails() {
System.out.println(“Brand: ” + brand + “, Year: ” + year);
}

// Public method to access private fields and methods
public void setDetails(String b, int y) {
brand = b;
year = y;
}

public void showDetails() {
displayDetails(); // private method accessed within the class
}
}

public class Main {
public static void main(String[] args) {
Car car = new Car();
car.setDetails(“Honda”, 2022);
car.showDetails();
// car.brand = “Toyota”; // ERROR: Cannot access private field
// car.displayDetails(); // ERROR: Cannot access private method
}
}

Here, the brand field and displayDetails method are private, so they can’t be accessed directly outside the Car class. We use public methods (setDetails and showDetails) to access them indirectly.

3. protected Access Specifier

The protected access specifier permits access to a member inside the same package additionally by subclasses, indeed if they are in several packages.

  • Class: A protected class can as it were be accessed inside the same package or by subclasses.
  • Method/Field: A protected method or field can be accessed within the same package or by subclasses (even if they are in different packages).

Example of protected access specifier:

package vehicles;

public class Car {
protected String brand; // protected field
protected int year; // protected field

// Protected method
protected void displayDetails() {
System.out.println(“Brand: ” + brand + “, Year: ” + year);
}
}

package vehicles;

public class SportsCar extends Car {
public void showDetails() {
brand = “Ferrari”; // Accessible due to protected access
year = 2023; // Accessible due to protected access
displayDetails(); // Accessible due to protected access
}
}

package main;

import vehicles.SportsCar;

public class Main {
public static void main(String[] args) {
SportsCar sportsCar = new SportsCar();
sportsCar.showDetails(); // Accessing protected method from subclass
// sportsCar.brand = “BMW”; // ERROR: Cannot access protected field outside package
}
}

In this example, the brand, year, and displayDetails members are protected in the Car class, so they can be accessed by a subclass (SportsCar) within the same package or from a subclass in another package.

4. Default (Package-Private) Access Specifier

When no access specifier is mentioned (i.e., no public, private, or protected), the member has default access (too known as package-private). This implies the member is accessible as it were inside the same package.

  • Class: A class with default access can as it were be accessed inside the same package.
  • Method/Field: A method or field with default access can only be accessed within the same package.

Example of Default Access Specifier:

package vehicles;

class Car { // Default class access (package-private)
String brand; // Default field access
int year; // Default field access

void displayDetails() { // Default method access
System.out.println(“Brand: ” + brand + “, Year: ” + year);
}
}

package vehicles;

public class Main {
public static void main(String[] args) {
Car car = new Car();
car.brand = “BMW”; // Accessible because it’s in the same package
car.year = 2022; // Accessible because it’s in the same package
car.displayDetails(); // Accessible because it’s in the same package
}
}

Here, the Car class, the brand field, and the displayDetails method are default, so they can only be accessed by classes within the same vehicles package.

Summary of Access Specifiers in Java:

Access SpecifierCan Access Within Same ClassCan Access Within Same PackageCan Access In Subclass (Different Package)Can Access From Anywhere
publicYesYesYesYes
privateYesNoNoNo
protectedYesYesYes (In subclass)No
Default (Package-Private)YesYesNoNo

 

Note:

  • Utilize public once you need the class, method, or field to be accessible from any other class.
  • Utilize private once you need the class members to be accessible as it were within the same class for information encapsulation.
  • Utilize protected after you need members to be accessible by subclasses or other classes within the same package.
  • Utilize default access (no modifier) after you need the members to be accessible as it were inside the same package.

This gives you adaptability in controlling the accessibility of your class members in Java. 

 

Leave a Comment