Java Access Modifiers [with Examples]

Java Tutorials

Summary: In this tutorial, you will learn about the significance of access modifiers and its types in Java with the help of the examples.

Access Modifiers in Java

In Java, access modifiers are keywords that determine the visibility and accessibility of a class, field, method, or constructor. There are four access modifiers in Java:

  1. public: This modifier allows the class, field, method, or constructor to be accessed from anywhere.
  2. protected: This modifier allows the class, field, method, or constructor to be accessed within the package in which it is declared, as well as by any subclass of the class in which it is declared.
  3. default (also known as package-private): This modifier allows the class, field, method, or constructor to be accessed within the package in which it is declared, but not from any other package. This is the default access level if no access modifier is specified.
  4. private: This modifier allows the class, field, method, or constructor to be accessed only within the class in which it is declared.

Imagine that you are creating a simple program to manage a bank account. You might define a BankAccount class with the following fields:

public class BankAccount {
    // fields
    private String accountNumber;
    private double balance;
    private String accountHolder;

    // constructor
    public BankAccount(String accountNumber, double balance, String accountHolder) {
        this.accountNumber = accountNumber;
        this.balance = balance;
        this.accountHolder = accountHolder;
    }
}

In this example, the accountNumber, balance, and accountHolder fields are all marked as private. This means that they can only be accessed within the BankAccount class.

However, you might want to allow other classes to retrieve the account balance and account holder name, but not the account number. To do this, you could add the following methods to the BankAccount class:

public class BankAccount {
    // fields
    private String accountNumber;
    private double balance;
    private String accountHolder;

    // constructor
    public BankAccount(String accountNumber, double balance, String accountHolder) {
        this.accountNumber = accountNumber;
        this.balance = balance;
        this.accountHolder = accountHolder;
    }

    // methods
    public double getBalance() {
        return balance;
    }

    public String getAccountHolder() {
        return accountHolder;
    }
}

Now, other classes can use the getBalance() and getAccountHolder() methods to access the balance and accountHolder fields, respectively. However, they cannot access the accountNumber field directly, since it is marked as private.

Here are some examples of how each access modifier can be used in Java:

1. public

This modifier allows the class, field, method, or constructor to be accessed from anywhere. For example:

public class MyClass {
    public int x;

    public void foo() {
        System.out.println("Hello, world!");
    }
}

In this example, the x field and the foo() method can be accessed from any other class. For example:

public class AnotherClass {
    void bar() {
        MyClass obj = new MyClass();
        obj.x = 10; // okay, because x is public
        obj.foo(); // okay, because foo() is public
    }
}

2. protected

This modifier allows the class, field, method, or constructor to be accessed within the package in which it is declared, as well as by any subclass of the class in which it is declared. For example:

public class MyClass {
    protected int y;

    protected void foo() {
        System.out.println("Hello, world!");
    }
}

public class MySubclass extends MyClass {
    void bar() {
        y = 10; // okay, because y is protected and MySubclass is a subclass of MyClass
        foo(); // okay, because foo() is protected and MySubclass is a subclass of MyClass
    }
}

In this example, the y field and the foo() method can be accessed from MySubclass, because MySubclass is a subclass of MyClass.

This example use inheritance in Java. You will learn about it in later tutorials.

3. default

This modifier allows the class, field, method, or constructor to be accessed within the package in which it is declared, but not from any other package. This is the default access level if no access modifier is specified. For example:

class MyClass {
    int z;

    void foo() {
        System.out.println("Hello, world!");
    }
}

class AnotherClass {
    void bar() {
        MyClass obj = new MyClass();
        obj.z = 10; // okay, because z has default access and AnotherClass is in the same package as MyClass
        obj.foo(); // okay, because foo() has default access and AnotherClass is in the same package as MyClass
    }
}

In this example, the z field and the foo() method can be accessed from AnotherClass, because both classes are in the same package.

4. private

This modifier allows the class, field, method, or constructor to be accessed only within the class in which it is declared. For example:

public class MyClass {
    private int w;

    private void foo() {
        System.out.println("Hello, world!");
    }
}

public class AnotherClass {
    void bar() {
        MyClass obj = new MyClass();
        obj.w = 10; // error, because w is private and cannot be accessed from AnotherClass
        obj.foo(); // error, because foo() is private and cannot be accessed from AnotherClass
    }
}

In this example, the w field and the qux() method can only be accessed from within the MyClass class. They cannot be accessed from any other class, including AnotherClass.

In general, you should try to use the most restrictive access modifier that makes sense for your class, field, method, or constructor. This helps to encapsulate the implementation details of your code and protect it from unintended use.


Leave a Reply

Your email address will not be published. Required fields are marked *