Java Reference
In-Depth Information
interface name followed by dot and then the constant namethe usual
way of referring to static members.
interface X {
int val = 1;
}
interface Y extends X {
int val = 2;
int sum = val + X.val;
}
Interface
Y
has two constants:
val
and
sum
. From inside
Y
, to refer to the
hidden
val
in its superinterface you must qualify it as
X.val
. Externally,
you can access the constants of
Y
by using the normal static forms of
Y.val
and
Y.sum
, and of course you can access
X
's
val
constant by using
X.val
.
These rules are, of course, identical to those concerning the inheritance
of static fields in classes.
When a class implements
Y
you can access the constants in
Y
as though
they were constants declared in the class. For example, given
class Z implements Y { }
you can do
System.out.println("Z.val=" + Z.val + ", Z.sum=" + Z.sum);
but there is no way to refer to
X.val
via
Z
. However, given an instance
of
Z
you can use an explicit cast to access
X.val
:
Z z = new Z();
System.out.println("z.val=" + z.val +