Java Reference
In-Depth Information
}
This program produces the output:
hello from Hyper
Suppose that a new version of class
Super
is produced:
class Super extends Hyper {
void hello() { System.out.println("hello from Super"); }
}
Then, if
Super
and
Hyper
are recompiled but not
Test
, then running the new binaries with
the existing binary of
Test
produces the output:
hello from Super
as you might expect.
13.4.7. Access to Members and Constructors
Changing the declared access of a member or constructor to permit less access may break
compatibility with pre-existing binaries, causing a linkage error to be thrown when these
binaries are resolved. Less access is permitted if the access modifier is changed from de-
fault access to
private
access; from
protected
access to default or
private
access; or from
public
access to
protected
, default, or
private
access. Changing a member or constructor to permit
less access is therefore not recommended for widely distributed classes.
Perhaps surprisingly, the binary format is defined so that changing a member or constructor
to be more accessible does not cause a linkage error when a subclass (already) defines a
method to have less access.
Example 13.4.7-1. Changing Accessibility
If the package
points
defines the class
Point
:
package points;
public class Point {
public int x, y;
protected void print() {
System.out.println("(" + x + "," + y + ")");
}
}