Java Reference
In-Depth Information
", ((Y)z).val=" + ((Y)z).val +
", ((X)z).val=" + ((X)z).val);
which prints out
z.val=2, ((Y)z).val=2, ((X)z).val=1
as you would expect. Again these are the same rules that apply to static
fields in extended classesit doesn't matter whether a class inherits a
static field from a superclass or a superinterface.
While all these rules are necessary from a language perspective, they
have little practical consequencethere are few reasons to hide existing
fields, and all accesses to static fields, whether class or interface, should
be via the name of the type in which that field is declared.
If an interface inherits two or more constants with the same name,
then any simple reference to the constant is ambiguous and results in a
compile-time error. For example, given the previous interface declara-
tions and the following:
interface C {
String val = "Interface C";
}
interface D extends X, C { }
then the expression D.val is ambiguousdoes it mean the integer val or
the String reference val ? Inside D you would have to explicitly use X.val
or C.val .
A class that implements more than one interface, or that extends a class
and implements one or more interfaces, can experience the same hid-
ing and ambiguity issues as an interface that extends more than one
interface. The class's own static fields can hide the inherited fields of the
 
Search WWH ::




Custom Search