Java Reference
In-Depth Information
no distinction drawn between
static
and non-
static
fields in field hiding whereas a dis-
tinction is drawn between
static
and non-
static
methods in method hiding.
type.
In this respect, hiding of fields is similar to hiding of methods.
If a field declaration hides the declaration of another field, the two fields need not have the
same type.
A class inherits from its direct superclass and direct superinterfaces all the non-private
fields of the superclass and superinterfaces that are both accessible to code in the class and
not hidden by a declaration in the class.
A
private
field of a superclass might be accessible to a subclass - for example, if both classes
are members of the same class. Nevertheless, a
private
field is never inherited by a subclass.
It is possible for a class to inherit more than one field with the same name. Such a situation
does not in itself cause a compile-time error. However, any attempt within the body of the
class to refer to any such field by its simple name will result in a compile-time error, be-
cause 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.
similarly, a value stored in a field of type
double
is always an element of the double value
set. It is not permitted for a field of type
float
to contain an element of the float-extended-
exponent value set that is not also an element of the float value set, nor for a field of type
double
to contain an element of the double-extended-exponent value set that is not also an
element of the double value set.
Example 8.3-1. Multiply Inherited Fields
A class may inherit two or more fields with the same name, either from two interfaces
or from its superclass and an interface. A compile-time error occurs on any attempt
to refer to any ambiguously inherited field by its simple name. A qualified name or