Java Reference
In-Depth Information
It is a compile-time error for the body of an interface declaration to declare two fields with
the same name.
The declared type of a field is denoted by the Type that appears in the field declaration,
followed by any bracket pairs that follow the Identifier in the declarator.
If the interface declares a field with a certain name, then the declaration of that field is said
to hide any and all accessible declarations of fields with the same name in superinterfaces
of the interface.
It is possible for an interface to inherit more than one field with the same name. Such a situ-
ation does not in itself cause a compile-time error. However, any attempt within the body
of the interface to refer to any such field by its simple name will result in a compile-time
error, because such a reference is ambiguous.
There might be several paths by which the same field declaration might be inherited from
an interface. In such a situation, the field is considered to be inherited only once, and it may
be referred to by its simple name without ambiguity.
Example 9.3-1. Ambiguous Inherited Fields
If two fields with the same name are inherited by an interface because, for example,
two of its direct superinterfaces declare fields with that name, then a single ambiguous
member results. Any use of this ambiguous member will result in a compile-time er-
ror. In the program:
Click here to view code image
interface BaseColors {
int RED = 1, GREEN = 2, BLUE = 4;
}
interface RainbowColors extends BaseColors {
int YELLOW = 3, ORANGE = 5, INDIGO = 6, VIOLET = 7;
}
interface PrintColors extends BaseColors {
int YELLOW = 8, CYAN = 16, MAGENTA = 32;
}
interface LotsOfColors extends RainbowColors, PrintColors {
int FUCHSIA = 17, VERMILION = 43, CHARTREUSE = RED+90;
}
the interface LotsOfColors inherits two fields named YELLOW . This is all right as long
as the interface does not contain any reference by simple name to the field YELLOW .
(Such a reference could occur within a variable initializer for a field.)
Search WWH ::




Custom Search