Sub topic 7
Control Method Access
An access modifier is a word that controls where a method can be called. Choosing the right access level lets other code use the methods it needs while keeping internal details hidden.
A package is a named group of Java classes. A subclass is a class built from another class. It can use certain methods from that class.
The four access levels
| Access level | Same class | Other classes in the same package | Subclass in another package | Unrelated class in another package |
|---|---|---|---|---|
| `public` | Yes | Yes | Yes | Yes |
| `protected` | Yes | Yes | Yes, with a restriction | No |
| No modifier | Yes | Yes | No | No |
| `private` | Yes | No | No | No |
A method with no access modifier has package-private access. Do not write a special word for it.
The table assumes the class that contains the method is itself accessible. For example, a method being public does not make its class public.
Try the access levels
This example uses two packages: bank and app. Create folders with those names. Save this file as bank/Account.java:
package bank;
public class Account {
public int balance() {
return readBalance();
}
int accountCode() {
return 42;
}
protected int bonusPoints() {
return 10;
}
private int readBalance() {
return 100;
}
}The balance method is public. It calls the private readBalance method from inside the same class. Code outside Account can ask for the balance, but it cannot call readBalance directly.
The accountCode method has no modifier, so other classes in bank can call it. The bonusPoints method is protected, so classes in bank can call it, and subclasses in other packages can use it with a restriction.
Now save this file as app/Main.java:
package app;
import bank.Account;
class RewardAccount extends Account {
public int points() {
return bonusPoints();
}
}
public class Main {
public static void main(String[] args) {
Account account = new Account();
RewardAccount rewards = new RewardAccount();
System.out.println(account.balance());
System.out.println(rewards.points());
// These calls would not compile:
// account.accountCode();
// account.bonusPoints();
// account.readBalance();
}
}The import line lets this file use the name Account for the class in the bank package. The word extends makes RewardAccount a subclass of Account.
With a Java development kit installed, run these commands from the folder containing bank and app:
javac bank/Account.java app/Main.java
java app.MainThe output is:
100
10The first call works because balance is public. The second works because points calls the protected method from inside the RewardAccount subclass.
The three commented-out calls would cause compile errors. Main is outside bank, and it is not a subclass of Account. It also cannot call the private method.
Choose an access level
Start with the smallest access level that meets your needs:
- Use
privatefor a helper used only inside the class. - Use package-private access when classes in the same package need to share a method.
- Use
protectedwhen subclasses may need the method. - Use
publicwhen code outside the package needs to call it.
A smaller access level can make a class easier to change. Outside code cannot depend directly on a private helper.
Exercise
Why would account.accountCode() fail in Main? How could you make that call work without moving Main into the bank package?
Show answer
accountCode has package-private access, but Main is in the app package. Add public before int in the accountCode declaration, then compile the files again.