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.5 Example: Default-Access Fields, Methods, and Constructors NAMES<br />

142<br />

However, this compilation unit cannot use or import points.PointList, which<br />

is not declared public and is therefore inaccessible outside package points.<br />

6.6.5 Example: Default-Access Fields, Methods, and Constructors<br />

If none of the access modifiers public, protected, orprivate are specified, a<br />

class member or constructor is accessible throughout the package that contains the<br />

declaration of the class in which the class member is declared, but the class member<br />

or constructor is not accessible in any other package.<br />

If a public class has a method or constructor with default access, then this<br />

method or constructor is not accessible to or inherited by a subclass declared outside<br />

this package.<br />

For example, if we have:<br />

package points;<br />

public class Point {<br />

public int x, y;<br />

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

public void moveAlso(int dx, int dy) { move(dx, dy); }<br />

}<br />

then a subclass in another package may declare an unrelated move method, with<br />

the same signature (§8.4.2) and return type. Because the original move method is<br />

not accessible from package morepoints, super may not be used:<br />

package morepoints;<br />

public class PlusPoint extends points.Point {<br />

public void move(int dx, int dy) {<br />

super.move(dx, dy); // compile-time error<br />

moveAlso(dx, dy);<br />

}<br />

}<br />

Because move of Point is not overridden by move in PlusPoint, the method<br />

moveAlso in Point never calls the method move in PlusPoint.<br />

Thus if you delete the super.move call from PlusPoint and execute the test<br />

program:<br />

import points.Point;<br />

import morepoints.PlusPoint;<br />

class Test {<br />

public static void main(String[] args) {<br />

PlusPoint pp = new PlusPoint();<br />

pp.move(1, 1);<br />

}<br />

}<br />

DRAFT

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

Saved successfully!

Ooh no, something went wrong!