@Override : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

@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;
   }

This page is posted
on the web at:

http://mindprod.com/jgloss/atoverride.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\atoverride.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[34.203.242.200]
You are visitor number