9.3.2
Examples of Field Declarations
INTERFACES
9.3.2 Examples of Field Declarations
The following example illustrates some (possibly subtle) points about field decla
rations.
9.3.2.1
Ambiguous Inherited Fields
If two fields with the same name are inherited by an interface because, for exam
ple, 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 error. Thus in the example:
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.)
Even if interface
PrintColors
were to give the value
3
to
YELLOW
rather than
the value
8
, a reference to field
YELLOW
within interface
LotsOfColors
would
still be considered ambiguous.
9.3.2.2
Multiply Inherited Fields
If a single field is inherited multiple times from the same interface because, for
example, both this interface and one of this interface's direct superinterfaces
extend the interface that declares the field, then only a single member results. This
situation does not in itself cause a compile time error.
In the example in the previous section, the fields
RED
,
GREEN
, and
BLUE
are
inherited by interface
LotsOfColors
in more than one way, through interface
RainbowColors
and also through interface
PrintColors
, but the reference to
field
RED
in interface
LotsOfColors
is not considered ambiguous because only
one actual declaration of the field
RED
is involved.
188
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved