Sub topic 8
Protected Access Across Package Boundaries
A package is a group of related Java classes. A subclass is a class that extends another class, called its parent class. A member is a field or method belonging to a class.
A protected member can be used within its declaring class’s package. It can also be used by a subclass in a different package, but with an important limit: the type used to access an instance member matters.
The rule in brief
Suppose Animal declares a protected field, and Dog extends Animal.
- Code in the
Animalpackage can access the field, even if that code is not in a subclass. - Code inside
Dog, in another package, can access the field on the currentDogobject. - Code inside
Dogcan also access the field through a reference whose type isDogor a subclass ofDog. - Code outside the
Animalpackage that is not in a subclass cannot access the field just because it has aDogobject.
A reference is a value that points to an object. A field is a variable stored in an object.
Try it with two packages
Create two directories, named animals and pets. Save each file below in the matching directory.
First, save this as animals/Animal.java:
package animals;
public class Animal {
protected String call = "hello";
}The package line names the package that contains the class. The field call is declared in Animal, so animals is the package that matters for same-package access.
Now save this as pets/Dog.java:
package pets;
import animals.Animal;
public class Dog extends Animal {
public void greet(Dog other) {
System.out.println(this.call);
System.out.println(other.call);
// Animal animal = other;
// System.out.println(animal.call); // Does not compile
}
}The import line lets Dog use the name Animal. The word extends makes Dog a subclass of Animal.
Inside greet, this.call reads the field on the current Dog. The expression other.call reads it on another object through a reference of type Dog. Both are allowed inside Dog.
Finally, save this as pets/Main.java:
package pets;
public class Main {
public static void main(String[] args) {
Dog first = new Dog();
Dog second = new Dog();
first.greet(second);
}
}From the directory containing animals and pets, compile and run the files:
javac animals/Animal.java pets/Dog.java pets/Main.java
java pets.MainThe output is:
hello
helloWhy does the declared type matter?
In Dog.java, remove the comment marks from these two lines:
Animal animal = other;
System.out.println(animal.call);Try compiling again. The second line causes an error. The actual object is still a Dog, but the variable animal has the declared type Animal. The declared type is the type written for a variable.
Across a package boundary, code in Dog cannot use an Animal-typed reference to access this protected instance field. It needs a reference of type Dog or a subclass of Dog.
Put the comment marks back before continuing.
A nearby class is not automatically allowed
Main and Dog are both in the pets package. But Main is not in Animal’s package and is not a subclass of Animal.
For example, adding this line to Main would cause a compile error:
System.out.println(first.call);Sharing a package with Dog does not grant access. For same-package access, Java checks the package where the protected member was declared: here, that is animals.
Check your understanding
Exercise
Inside Dog, why does other.call work when other has type Dog, but animal.call fails when animal has type Animal?
Show answer
Dog is a subclass in a different package. It can access the protected instance field through a Dog reference, but not through an Animal reference, even if both references point to a Dog object.
Exercise
Could a class in the animals package access call through an Animal reference without extending Animal?
Show answer
Yes. The field is declared in the animals package, so classes in that package can access it.