8.3.3
Examples of Field Declarations
CLASSES
}
void printV() {
System.out.println((super.v + Frob.v)/2);
}
}
It compiles and prints:
2.5
Even if two distinct inherited fields have the same type, the same value, and
are both
final
, any reference to either field by simple name is considered ambig
uous and results in a compile time error. In the example:
interface Color { int RED=0, GREEN=1, BLUE=2; }
interface TrafficLight { int RED=0, YELLOW=1, GREEN=2; }
class Test implements Color, TrafficLight {
public static void main(String[] args) {
System.out.println(GREEN);
//
compile time error
System.out.println(RED);
//
compile time error
}
}
it is not astonishing that the reference to
GREEN
should be considered ambiguous,
because class
Test
inherits two different declarations for
GREEN
with different
values. The point of this example is that the reference to
RED
is also considered
ambiguous, because two distinct declarations are inherited. The fact that the two
fields named
RED
happen to have the same type and the same unchanging value
does not affect this judgment.
8.3.3.4
Example: Re inheritance of Fields
If the same field declaration is inherited from an interface by multiple paths, the
field is considered to be inherited only once. It may be referred to by its simple
name without ambiguity. For example, in the code:
public interface Colorable {
int RED = 0xff0000, GREEN = 0x00ff00, BLUE = 0x0000ff;
}
public interface Paintable extends Colorable {
int MATTE = 0, GLOSSY = 1;
}
class Point { int x, y; }
class ColoredPoint extends Point implements Colorable {
. . .
}
154
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