Java Reference
In-Depth Information
confusion, simply avoid hiding .
A class that hides a field with one whose accessibility is more restrictive than that of the hidden
field, as in our original program, violates the principle of subsumption , also known as the Liskov
Substitution Principle [Liskov87] . This principle says that everything you can do with a base class,
you can also do with a derived class. Subsumption is an integral part of the natural mental model of
object-oriented programming. Whenever it is violated, a program becomes more difficult to
understand. There are other ways that hiding one field with another can violate subsumption: if the
two fields are of different types, if one field is static and the other isn't, if one field is final and the
other isn't, if one field is constant and the other isn't, or if both are constant and have different
values.
For language designers, consider eliminating the possibility of hiding: for example, by making all
fields implicitly private. If this seems too draconian, at least consider restricting hiding so that it
preserves subsumption.
In summary, hiding occurs when you declare a field, a static method, or a nested type whose name
is identical to an accessible field, method, or type, respectively, in a superclass. Hiding is confusing;
avoid it. Hiding fields in a manner that violates subsumption is especially harmful. More generally,
avoid name reuse other than overriding.
< Day Day Up >
 
 
Search WWH ::




Custom Search