Sub topic 9
Apply Access Rules Across Packages
When a subclass lives in a different folder from its parent, Java's access rules change. This chapter shows which members a subclass can use across that boundary.
What a Package Is
A package is a group of related classes stored together. In Java, a package is usually a folder, and each file starts with a package line that names it.
An access modifier is a keyword that controls who can use a field or method. Java has four levels.
The Four Access Levels
| Modifier | Same class | Same package | Subclass in another package | Any class |
|---|---|---|---|---|
| `private` | Yes | No | No | No |
| (no keyword) | Yes | Yes | No | No |
| `protected` | Yes | Yes | Yes (with limits) | No |
| `public` | Yes | Yes | Yes | Yes |
The "no keyword" level is called package-private. It means only classes in the same package can see the member.
A Worked Example
We will use two packages: shapes holds the parent class, and app holds the subclass and the main program.
Create this file at shapes/Shape.java:
package shapes;
public class Shape {
private String secret = "hidden";
String packageNote = "same package only";
protected String label = "shape";
public String name = "generic";
protected String describe() {
return "I am a " + label;
}
}Create this file at app/Circle.java:
package app;
import shapes.Shape;
public class Circle extends Shape {
public void show() {
System.out.println(label); // protected field: allowed
System.out.println(name); // public field: allowed
System.out.println(describe()); // protected method: allowed
// System.out.println(secret); // private: compile error
// System.out.println(packageNote); // package-private: compile error
}
}Create this file at app/Main.java:
package app;
public class Main {
public static void main(String[] args) {
Circle c = new Circle();
c.show();
}
}From the project folder, compile and run:
javac -d out shapes/Shape.java app/Circle.java app/Main.java
java -cp out app.MainThe output is:
shape
generic
I am a shapeThe subclass Circle can use label, name, and describe(). It cannot use secret or packageNote.
> Note the word "protected" in the table. A subclass gets access to protected members through inheritance, not through any object it happens to hold.
The Protected Trap
Suppose Circle tries this:
Shape other = new Shape();
System.out.println(other.label); // compile errorThis fails. Inside Circle, you may use label on the Circle object itself (or on another Circle). You may not use it on a plain Shape object, because Circle does not own that object's inherited state.
Exercise
Exercise
In the example above, Main (in package app) writes Shape s = new Shape(); and then s.name. Will this compile? What about s.label?
Show answer
Both are fine if they are inside Main? No. Main is not a subclass of Shape, so s.label fails: protected members are visible only inside the same package or to subclasses. The public field s.name compiles. Only the subclass rule applies to protected, so s.label fails in Main but would still fail in Circle when using a plain Shape reference.