Java Reference
In-Depth Information
The short, and perhaps surprising, answer is that it does nothing at all.
The less short (and flippant) answer is that, like all annotations, it has no direct
effect, but instead acts as additional information about the method that it annotates
—in this case, it denotes that a method overrides a superclass method.
This acts as a useful hint to compilers and integrated development environments
(IDEs)—if a developer has misspelled the name of a method that she intended to be
an override of a superclass method, then the presence of the @Override annotation
on the misspelled method (which does not override anything) alerts the compiler to
the fact that something is not right.
Annotations are not allowed to alter program semantics—instead, they provide
optional metadata. In its strictest sense, this means that they should not affect pro‐
gram execution and instead can only provide information for compilers and other
pre-execution phases.
The platform defines a small number of basic annotations in java.lang . The origi‐
nal set were @Override , @Deprecated , and @SuppressWarnings —which were used
to indicate that a method was overriden, deprecated, or that it generated some com‐
piler warnings that should be suppressed.
m
e
These were augmented by @SafeVarargs in Java 7 (which provides extended warn‐
ing suppression for varargs methods) and @FunctionalInterface in Java 8. This
last annotation indicates an interface can be used as a target for a lambda expression
—it is a useful marker annotation although not mandatory, as we will see.
Annotations have some special properties, compared to regular interfaces:
• All (implicitly) extend java.lang.annotation.Annotation
• May not be generic
• May not extend any other interface
• May only define zero-arg methods
• May not define methods that throw exceptions
• Have restrictions on the return types of methods
• Can have a default return value for methods
Deining Custom Annotations
Defining custom annotation types for use in your own code is not that hard. The
@interface keyword allows the developer to define a new annotation type, in much
the same way that class or interface are used.
Search WWH ::




Custom Search