@Override : Java Glossary

go to home page punctuation words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2009 2008-02-17 Roedy Green, Canadian Mind Products
@Override
To override means to provide a replacement method in a new class for one in the superclass. The superclass too will use your new method in place of its own when dealing with objects of your type, though it will continue to use its own method for objects purely of its own type. Be careful. The rules for figuring out exactly which version of a method get invoked are baroque.

Your overridden method must have the same return type, though with JDK 1.5+, you have the option of covariance where your overriding method can be a subclass or the root method’s return type.

When overriding a method, be careful to exactly match the method signature, or you will end up just adding another method, rather than replacing the superclass version. You are permitted to widen the scope however, e.g. make your new method public where the original was default.

Using @Override

There is no override keyword to warn the compiler your method is intended to override a method in the base class. However, since JDK 1.5+, there is an @Override annotation. @Override serves as documentation for the reader and a double check in the compiler. The compiler will warn you if your method is not overriding properly, usually because the two method names are not spelled exactly the same way. Note it is @Override with capital O and double r, not @overide. Don’t confuse the @Override annotation with the unofficial @override Javadoc command. Here is how you would use the @Override annotation to indicate that a method is overriding another in the base class.
/**
 * Get String presentation for this Thing object.
 * @return human readable summary of fields in Thing object.
 */
@Override
public String toString()
   {
   return desc + " " + quantity + " " + losses;
   }

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/atoverride.html J:\mindprod\jgloss\atoverride.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.107]
You are visitor number 11.