4.7 Accessibility Modifiers for Top-level Classes and Interfaces

Top-level classes and interfaces within a package can be declared as public. This means that they are accessible from everywhere, both inside and outside of their package. If the accessibility modifier is omitted, then they are only accessible in the package and not in any other packages or subpackages. This is called package or default accessibility.

Accessibility modifiers for nested classes and interfaces are discussed in Section 7.1 on page 284.

Example 4.7 Accessibility Modifiers for Classes and Interfaces
// File: Clown.java
package wizard.pandorasBox;                      // (1) Package declaration

import wizard.pandorasBox.artifacts.Ailment;     // (2) Importing class

public class Clown implements Magic {
    LovePotion tlc;                              // (3) Class in same package
    wizard.pandorasBox.artifacts.Ailment problem;// (4) Fully qualified class name
    Clown() {
        tlc = new LovePotion("passion");
        problem = new Ailment("flu");            // (5) Abbreviated class name
    }
    public void levitate()  { System.out.println("Levitating"); }
    public void mixPotion() { System.out.println("Mixing " + tlc); }
    public void healAilment() { System.out.println("Healing " + problem); }

    public static void main(String[] args) {     // (6)
        Clown joker = new Clown();
        joker.levitate();
        joker.mixPotion();
        joker.healAilment();
    }
}

interface Magic { void levitate(); }             // (7)
_________________________________________________________________________________
// File: LovePotion.java
package wizard.pandorasBox;                // (1) Package declaration

public class LovePotion {                  // (2) Accessible outside package
    String potionName;
    public LovePotion(String name) { potionName = name; }
    public String toString() { return potionName; }
}
_________________________________________________________________________________
// File: Ailment.java
package wizard.pandorasBox.artifacts;      // (1) Package declaration

public class Ailment {                     // (2) Accessible outside package
    String ailmentName;
    public Ailment(String name) { ailmentName = name; }
    public String toString() { return ailmentName; }
}
_________________________________________________________________________________
// File: Baldness.java
package wizard.spells;                       // (1)Package declaration

import wizard.pandorasBox.*;                 // (2) Import of classes/interface
import wizard.pandorasBox.artifacts.*;       // (3) Import from subpackage

public class Baldness extends Ailment {      // (4) Abbreviated name for Ailment
    wizard.pandorasBox.LovePotion tlcOne;    // (5) Fully qualified name
    LovePotion tlcTwo;                       // (6) Class in same package
    Baldness(String name) {
        super(name);
        tlcOne = new wizard.pandorasBox.     // (7) Fully qualified name
                     LovePotion("romance");
        tlcTwo = new LovePotion();           // (8) Class in same package
    }
}

class LovePotion // implements Magic         // (9) Not accessible
    { public void levitate(){} }

Compiling and running the program from the current working directory results in the following:

>javac -d . Clown.java Ailment.java Baldness.java
>java wizard.pandorasBox.Clown
Levitating
Mixing passion
Healing flu

In Example 4.7, the class Clown and the interface Magic are placed in a package called wizard.pandorasBox. The public class Clown is accessible from everywhere. The Magic interface has default accessibility, and can only be accessed within the package wizard.pandorasBox. It is not accessible from other packages, not even in any packages nested in this package.

The class LovePotion is also placed in the package called wizard.pandorasBox. The class has public accessibility and is, therefore, accessible from other packages. The two files Clown.java and LovePotion.java demonstrate how several compilation units can be used to group classes in the same package.

The class Clown, from the file Clown.java, uses the class Ailment. The example shows two ways in which a class can access classes from other packages:

  1. Denote the class by its fully qualified class name, as shown at (4) (wizard.pandorasBox.artifacts.Ailment).

  2. Import the class explicitly from the package wizard.pandorasBox.artifacts as shown at (2) and use the simple class name Ailment as shown at (5).

In the file Baldness.java at (9), the class LovePotion wishes to implement the interface Magic from the package wizard.pandorasBox, but cannot do so, although the file imports from this package. The reason is that the interface Magic has default accessibility and can, therefore, only be accessed within the package wizard.pandorasBox.

Just because the class is accessible, it does not necessarily mean that members of the class are also accessible. Member accessibility is governed separately from class accessibility, as explained in Section 4.5.

Table 4.2. Summary of Accessibility Modifiers for Classes and Interfaces

Modifiers

Top-level Classes and Interfaces

default (no modifier)

Accessible in its package (package accessibility)

public

Accessible anywhere