Java Reference
In-Depth Information
• If the access is by a superclass constructor invocation super(. . .) or by a qualified su-
perclass constructor invocation of the form E .super(. . .) , where E is a Primary ex-
pression, then the access is permitted.
• If the access is by an anonymous class instance creation expression of the form new
C (. . .){...} or by a qualified class instance creation expression of the form E .new C (. .
.){...} , where E is a Primary expression, then the access is permitted.
• Otherwise, if the access is by a simple class instance creation expression of the
form new C (. . .) or by a qualified class instance creation expression of the form
E .new C (. . .) , where E is a Primary expression, then the access is not permitted.
A protected constructor can be accessed by a class instance creation expression (that
does not declare an anonymous class) only from within the package in which it is
defined.
Example 6.6.2-1. Access to protected Fields, Methods, and Constructors
Consider this example, where the points package declares:
Click here to view code image
package points;
public class Point {
protected int x, y;
void warp(threePoint.Point3d a) {
if (a.z > 0) // compile-time error: cannot access a.z
a.delta(this);
}
}
and the threePoint package declares:
Click here to view code image
package threePoint;
import points.Point;
public class Point3d extends Point {
protected int z;
public void delta(Point p) {
p.x += this.x; // compile-time error: cannot access p.x
p.y += this.y; // compile-time error: cannot access p.y
}
public void delta3d(Point3d q) {
q.x += this.x;
q.y += this.y;
q.z += this.z;
Search WWH ::




Custom Search