Access modifiers in Java

Access modifiers in Java define the visibility or scope of a class, method, or variable in a program. They allow developers to control who can access a particular member of a class—whether it’s limited to the same class, package, or accessible globally.

Ever wondered how Java ensures your sensitive data stays private and shared features remain accessible? The secret lies in access modifiers. Before learning the types of access modifiers let's assume a bank application:

  • The customer's bank balance is only accessible within the bank’s system (private access).

  • Employee benefits information might only be accessible to authorized personnel (protected access).

  • Bank branch locations, which everyone can access (public access).

  • Internal processing methods accessible within the department (default access).

Let's explore each modifier in detail:

Types of access modifiers in Java

There are 4 access modifiers in Java:

  1. private: This access modifier ensures that a member can only be accessed from within the class.

  2. public: This access modifier allows the data variable or method to be accessed anywhere. A variable can be accessed within the class or outside the class.

  3. protected: In a child class, the protected modified variable can be accessed within the package and outside the package. Protected allows access within the same package and also to subclasses, even if they are in a different package.

  4. default: The default access modified variable can only be accessed within the same package. The variable cannot be accessed outside the package. If no access modifier is mentioned, then the default modifier is automatically added.

Modifier Class Package Sub-class Global
public Yes Yes Yes Yes
private Yes No No No
default Yes Yes No No
protected Yes Yes Yes No

Note: Use protected with caution, especially in large projects. It can lead to unintended access when subclasses or packages expand, potentially exposing sensitive functionality. Always assess if a more restrictive modifier fits your needs.

Now that we’ve outlined the modifiers, let’s see how they work in a real-world scenario.

Real-world example

Imagine a bank system where:

  1. Private access is used to protect sensitive data like account balance.

  2. Default access is used for operations like updating transaction history within the same package.

  3. Protected access is used for features shared with specialized accounts (e.g., savings, current).

  4. Public access is used for general services like displaying account details.

Code example

// Bank Account class demonstrating access modifiers
class BankAccount {
// 1. Private: Sensitive data (only accessible within the class)
private double accountBalance = 1000.00;
private void showBalance() {
System.out.println("Private: Account Balance is $" + accountBalance);
}
// 2. Default: Internal package operations
String accountHolder = "John Doe";
void updateTransactionHistory() {
System.out.println("Default: Transaction history updated for " + accountHolder);
}
// 3. Protected: Shared access for specialized accounts
protected String accountType = "Savings";
protected void calculateInterest() {
System.out.println("Protected: Interest calculated for " + accountType + " account.");
}
// 4. Public: General services
public void displayAccountDetails() {
System.out.println("Public: Account Holder: " + accountHolder);
System.out.println("Public: Account Type: " + accountType);
}
}
// Specialized account class inheriting BankAccount
class PremiumAccount extends BankAccount {
void premiumBenefits() {
System.out.println("Accessing Protected: Premium benefits for " + accountType + " account.");
}
}
// Main Class
public class BankSystem {
public static void main(String[] args) {
BankAccount account = new BankAccount();
// Public: Accessible anywhere
account.displayAccountDetails();
// Default: Accessible within the same package
account.updateTransactionHistory();
// Protected: Accessible within the package or through inheritance
PremiumAccount premium = new PremiumAccount();
premium.calculateInterest();
premium.premiumBenefits();
// Private: Not accessible outside the class
// account.showBalance(); // ERROR: Private access
}
}

Explanation

  • Private:

    • accountBalance and showBalance ensure sensitive details like balance are secure.

    • These can only be accessed or modified through specific methods in the class.

  • Default:

    • accountHolder and updateTransactionHistory are accessible only within the same package, ideal for internal bank processes.

  • Protected:

    • accountType and calculateInterest allow shared features like interest calculation for different account types, even for subclasses like PremiumAccount.

  • Public:

    • displayAccountDetails provides open access to basic account details, such as the holder's name and account type.

Add a public method to the BankAccount class that allows securely updating the accountBalance (private variable) with input validation to ensure the balance cannot be negative, and call this method from the main class.

Note: Overusing inheritance can lead to tight coupling and make maintenance difficult in deep hierarchies.

Key takeaways

  • Use private for sensitive data and provide controlled access via getter and setter methods.

  • Avoid using public unless necessary to reduce the risk of unintended interactions.

  • Use protected for methods intended to be extended by subclasses.

  • Default access is useful for internal organization but can lead to unintentional package-level exposure.

  • Overusing public can compromise security.

  • Relying on default access can cause package dependency issues during refactoring.

Unlock your Java potential and take control of your coding journey! Start learning Java today to explore more than access modifiers, build Java-based applications, and become a Java developer now!

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What are the 4 access modifiers in Java?

The four access modifiers in Java are:

  1. Private: Members are accessible only within the class they are declared.
  2. Default (no keyword): Members are accessible within the same package.
  3. Protected: Members are accessible within the same package and in subclasses (even in other packages).
  4. Public: Members are accessible from anywhere.

What is public vs protected vs private?

  • Public: The member can be accessed from anywhere in the application.
  • Protected: The member can be accessed within the same package and subclasses in other packages.
  • Private: The member is accessible only within the class where it is declared.

What are the 12 modifiers in Java?

Java modifiers are divided into two categories:

  • Access Modifiers (4): private, default, protected, public.
  • Non-Access Modifiers (8): final, static, abstract, synchronized, volatile, transient, native, strictfp.

What do you mean by access modifiers?

Access modifiers in Java define the scope and visibility of a class, method, or variable. They control which parts of the program can access a particular member, ensuring encapsulation and security.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved