Java Reference
In-Depth Information
class Test extends Super {
public static void main(String[] args) {
String s = new Test().h;
System.out.println(s);
}
}
is compiled and executed, it produces the output:
Hyper
Suppose that a new version of class
Super
is then compiled:
class Super extends Hyper { char h = 'h'; }
If the resulting binary is used with the existing binaries for
Hyper
and
Test
, then the
output is still:
Hyper
even though compiling the source for these binaries:
class Hyper { String h = "Hyper"; }
class Super extends Hyper { char h = 'h'; }
class Test extends Super {
public static void main(String[] args) {
String s = new Test().h;
System.out.println(s);
}
}
would result in a compile-time error, because the
h
in the source code for
main
would
now be construed as referring to the
char
field declared in
Super
, and a
char
value can't
be assigned to a
String
.
Deleting a field from a class will break compatibility with any pre-existing binaries that
reference this field, and a
NoSuchFieldError
will be thrown when such a reference from a pre-
existing binary is linked. Only
private
fields may be safely deleted from a widely distributed
class.
For purposes of binary compatibility, adding or removing a field
f
whose type involves type
removal) of a field of the same name whose type is the erasure (§
4.6
) of the type of
f
.