@Override
annotation helps prevent a common error when
overriding Object.equals()
.
The common error is declaring the wrong signature for Object.equals()
:
class Blob { public boolean equals(Blob b) { // blah blah blah } }
This does not override Object.equals()
, which has signature
public boolean equals(Object o)
Blob.equals()
does not override Object.equals()
because they aren't the
same method.
The @Override
annotation helps prevent this error. If Blob.equals()
is
tagged with @Override
,
class Blob { @Override public boolean equals(Blob b) { // blah blah blah } }
the compiler checks that it actually overrides a method in an ancestor and issue an error if it doesn't, as it would in this case (Equality Testing and Inheritance, page 173 (7th ed.) or 194 (8th ed.)).
The substitution principle is a test for inheritance-hierarchy quality with respect to polymorphism. If any descendant-class instance can successfully replace any ancestor-class instance for classes in an inheritance hierarchy, then the inheritance hierarchy is well-designed with respect to polymorphism. (Polymorphism, 158 (7th ed.) or 179 (8th ed.)) Substitution-principle failures indicate places where the classes or the inheritance hierarchy need to be reconsidered, at least with repect to polymorphism.
This page last modified on 31 March 2008. |
|