Sub topic 11
Package-private and protected access
Assume you know classes, inheritance, methods, and basic package declarations. This chapter focuses on how package boundaries affect access to classes and members.
Package-private access
A member with no access modifier is package-private. It can be used by any class in the same package, but not by classes in other packages.
package com.example.shop.model;
class PriceFormatter {
static String format(double price) {
return String.format("%.2f", price);
}
}
public class Product {
private final double price;
public Product(double price) {
this.price = price;
}
public String displayPrice() {
return PriceFormatter.format(price);
}
}PriceFormatter is also package-private because it has no modifier. A class in com.example.shop cannot import it:
package com.example.shop;
import com.example.shop.model.PriceFormatter; // Compile-time errorPackage-private access is useful for implementation details shared by several classes in one package. It keeps those details out of the package's public API.
A top-level class can be either public or package-private. It cannot be private or protected. A package-private top-level class is visible only within its own package.
Protected access
A protected member has two access rules:
- Any class in the declaring class's package can use it.
- A subclass in another package can use it through inheritance.
package com.example.shop.model;
public class Product {
protected String category;
protected void printCategory() {
System.out.println(category);
}
}A class in the same package can access category directly, even if it is not a subclass:
package com.example.shop.model;
public class ProductPrinter {
public void print(Product product) {
System.out.println(product.category);
product.printCategory();
}
}A subclass in another package can access the protected member through itself or super:
package com.example.shop.discount;
import com.example.shop.model.Product;
public class DiscountedProduct extends Product {
public DiscountedProduct(String category) {
this.category = category;
}
public void showCategory() {
super.printCategory();
System.out.println(category);
}
}However, code in that subclass cannot use the protected member through an unrelated Product reference:
package com.example.shop.discount;
import com.example.shop.model.Product;
public class InvalidDiscount {
public void print(Product product) {
System.out.println(product.category); // Compile-time error
}
}The subclass relationship grants inherited access, not general access to every instance of the declaring type. This rule often causes confusion.
Package boundaries and common mistakes
Java determines package access from the package declaration, not merely from similar directory names. Two classes belong to the same package only when their declarations name the same package.
| Declaration | Same package | Subclass in another package | Unrelated class in another package |
|---|---|---|---|
| package-private member | Yes | No | No |
| protected member | Yes | Yes, through inheritance | No |
| public member | Yes | Yes | Yes |
| private member | No, except the declaring class | No | No |
Summary
Package-private access is limited to the declaring package. Protected access includes the declaring package and subclasses in other packages, but those external subclasses must use inherited access through themselves or super. Neither modifier makes a member part of the unrestricted public API.
Exercise
A subclass in another package receives a Product product parameter. Can it read product.category when category is protected?
Show answer
No. It can read its inherited category through this or access it with super, but it cannot use a protected member through an arbitrary Product reference.