10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6.6.3 An Example of Access Control NAMES<br />

140<br />

6.6.2.2 Qualified Access to a protected Constructor<br />

Let C be the class in which a protected constructor is declared and let S be the<br />

innermost class in whose declaration the use of the protected constructor<br />

occurs. <strong>The</strong>n:<br />

• If the access is by a superclass constructor invocation super(. ..) or by a<br />

qualified superclass constructor invocation of the form E.super(. ..), where<br />

E is a Primary expression, then the access is permitted.<br />

• If the access is by an anonymous class instance creation expression of the<br />

form new C(. ..){...} or by a qualified class instance creation expression of<br />

the form E.new C(. ..){...}, where E is a Primary expression, then the<br />

access is permitted.<br />

• Otherwise, if the access is by a simple class instance creation expression of<br />

the form new C(...) or by a qualified class instance creation expression of the<br />

form E.new C(. ..), where E is a Primary expression, then the access is not<br />

permitted. A protected constructor can be accessed by a class instance creation<br />

expression (that does not declare an anonymous class) only from within<br />

the package in which it is defined.<br />

6.6.3 An Example of Access Control<br />

For examples of access control, consider the two compilation units:<br />

package points;<br />

class PointVec { Point[] vec; }<br />

and:<br />

package points;<br />

public class Point {<br />

protected int x, y;<br />

public void move(int dx, int dy) { x += dx; y += dy; }<br />

public int getX() { return x; }<br />

public int getY() { return y; }<br />

}<br />

which declare two class types in the package points:<br />

• <strong>The</strong> class type PointVec is not public and not part of the public interface<br />

of the package points, but rather can be used only by other classes in the<br />

package.<br />

• <strong>The</strong> class type Point is declared public and is available to other packages. It<br />

is part of the public interface of the package points.<br />

DRAFT

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!