About the difference between the @override after 1.5 and 1.6

Source: Internet
Author: User

@Override Annotations are annotations that act on the source code and are used to indicate that the method of the annotation overrides the method of the parent type, but this annotation differs between 1.5 and 1.6 and later. 1.5, it can only be used when inheriting a class, overriding a method in the parent class, and when implementing a method in an interface, it is not possible to use the annotation, starting from 1.6, to support methods that implement the parent interface. However, in the @override source code document, 1.6 did not explain this change, until 1.7 was explained.

Below are the source codes for 1.5 and 1.6:

Package Java.lang;import java.lang.annotation.*;/** * Indicates, a method declaration is intended to override a * meth OD declaration in a superclass.  If a method is annotated with * the annotation type but does not override a superclass method, * compilers is required t o Generate an error message. * * @author  Joshua Bloch * @since 1.5 */@Target (Elementtype.method) @Retention (retentionpolicy.source) public @ Interface Override {}
This means that a method declaration intends to override a method declaration in a superclass. If the method uses this annotation type for annotations but does not override the superclass method, the compiler generates an error message.

Here is the source code for 1.7:

Package Java.lang;import java.lang.annotation.*;/** * Indicates, a method declaration is intended to override a * meth OD declaration in a supertype.  If a method is annotated with * This annotation type compilers be required to generate an error * message unless at least One of the following conditions hold: * * <ul><li> * The method does override or implement a method declared In A * supertype. * </li><li> * The method has a signature that's override-equivalent to that of * any public method declared I n {@linkplain Object}.  * </li></ul> * * @author  Peter von der ahé* @author  Joshua Bloch * @jls 9.6.1.4 Override * @since 1.5 */@Target (Elementtype.method) @Retention (retentionpolicy.source) public @interface Override {}
This means that a method declaration intends to override a method declaration in the parent type. If the method uses this annotation type for annotations, the compiler must generate an error message unless one of the following two conditions is met:

    • This method overrides or implements a method declared in the parent type.
    • The signature of the method is equivalent to a public method override (Override-equivalent) declared in the object class.

The meaning of overriding equivalence can be seen in http://stackoverflow.com/questions/16207386/ What-is-override-equivalence-and-how-is-it-related-to-override


Notice the change of two words:

Indicates that a method declaration was intended to the override a method declaration in a superclass.

Indicates that a method declaration was intended to the override a method declaration in a supertype.

That is, 1.5 is superclass, then 1.7 is supertype. One is the parent class and one is the parent type. Why 1.6 of the document does not modify this change, but in essence, it supports the implementation of the interface method can use @override, it is unknown, may not be timely update the document it.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

About the difference between the @override after 1.5 and 1.6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.