@Override : Java Glossary

@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 the subclass type, though it will continue to use its own method for objects purely of the superclass type. Be careful. The rules for figuring out exactly which version of a method get invoked can be confusing. Experiment with debugging output until you are sure you understand how it works.

Your overridden method must have the same return type, though with Java version 1.5 or later, 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 Java version 1.5 or later, 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 @Overridewith 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. Note it is not inside the Javadoc comment!

/**
 * 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
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.210]
You are visitor number 11.