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.

8.2.1 Examples of Inheritance CLASSES<br />

194<br />

class Point3d extends Point {<br />

int z;<br />

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

super.move(dx, dy); z += dz; totalMoves++;<br />

}<br />

}<br />

the class variable totalMoves can be used only within the class Point; it is not<br />

inherited by the subclass Point3d. A compile-time error occurs because method<br />

move of class Point3d tries to increment totalMoves.<br />

8.2.1.4 Accessing Members of Inaccessible Classes<br />

Even though a class might not be declared public, instances of the class might be<br />

available at run time to code outside the package in which it is declared by means<br />

a public superclass or superinterface. An instance of the class can be assigned to<br />

a variable of such a public type. An invocation of a public method of the object<br />

referred to by such a variable may invoke a method of the class if it implements or<br />

overrides a method of the public superclass or superinterface. (In this situation,<br />

the method is necessarily declared public, even though it is declared in a class<br />

that is not public.)<br />

Consider the compilation unit:<br />

package points;<br />

public class Point {<br />

public int x, y;<br />

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

x += dx; y += dy;<br />

}<br />

}<br />

and another compilation unit of another package:<br />

DRAFT<br />

package morePoints;<br />

class Point3d extends points.Point {<br />

public int z;<br />

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

super.move(dx, dy); z += dz;<br />

}<br />

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

move(dx, dy, 0);<br />

}<br />

}

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

Saved successfully!

Ooh no, something went wrong!